---
source_url: https://www.pubnub.com/docs/general/setup/application-setup
title: Application Setup
updated_at: 2026-05-29T11:09:31.617Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Application Setup

Before setup, review the key concepts of User ID and SDK initialization.

:::note User ID / UUID
User ID is also referred to as **UUID/uuid** in some APIs and server responses but **holds the value** of the **userId** parameter you [set during initialization](https://www.pubnub.com/docs/general/setup/users-and-devices#set-the-user-id).
:::

## User IDs

Set a User ID to connect to PubNub.

Use one User ID per user across devices, or one per client. If users connect from multiple devices at the same time, use the same User ID on each device. Presence uses the User ID to show online status.

For client-side SDKs, get the User ID from your server after the user signs in. See [Users & Devices](https://www.pubnub.com/docs/general/setup/users-and-devices) for user authentication patterns and multi‑device session management.

## SDK integration and initialization

To use PubNub, install an SDK, initialize a PubNub object, and add event listeners.

### Include or install

Each platform offers more than one way to add the PubNub SDK to your app. Choose the method that fits your environment.

#### JavaScript

```javascript
// Using the PubNub CDN, add the SDK to your web application.

<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.29.9.js"></script>

// If you're using the PubNub Node.js SDK, use the command `npm install pubnub`
```

#### Swift

To integrate PubNub into your Xcode project using Swift Package Manager, specify it in the dependencies list of your Package.swift file:

```swift
dependencies: [
  .package(url: "https://github.com/pubnub/swift.git", from: "3.1.0")
]
```

Or, in Xcode, navigate to File > Swift Packages > Add Package Dependency, then enter `https://github.com/pubnub/swift.git` for the package repository URL. You can use defaults for the remaining steps.

#### Objective-C

CocoaPods is the easiest way to install the PubNub Objective-C SDK in your iOS app. (Check the [Objective-C SDK documentation](https://www.pubnub.com/docs/sdks/objective-c) for other installation options, and the latest SDK version.)

1. In the project's root folder, create a Podfile using pod init (requires CocoaPods 1.0.0 or above).
2. Open Podfile in an editor. Add pod "PubNub", "~> 4" to the target section, and replace MyObjcProject with your project name. 1platform :ios, '9.0' # (or '10.0')2 3target 'MyObjcProject' do4 use_frameworks!5 pod "PubNub", "~> 4"6end
3. In the same directory as the Podfile, enter the command: pod install.
4. In Xcode, open the workspace (.xcworkspace) file, not the project (.xcodeproj) file.

#### Java

You can include the PubNub SDK in your Android app using Gradle.

1. Insert the PubNub implementation dependency to your dependencies section in the build.gradle file of your project. Your list of existing dependencies may differ. 1dependencies {2 // some existing dependencies3 implementation fileTree(dir: 'libs', include: ['*.jar'])4 5 // add PubNub Kotlin SDK here at the bottom6 implementation group: 'com.pubnub', name: 'pubnub-kotlin', version: '9.0.0'7 8 // Alternatively, you can use the PubNub Java SDK (named 'pubnub-gson'):9 // implementation group: 'com.pubnub', name: 'pubnub-gson', version: '9.0.0'10 // Please note that only one of the PubNub SDKs may be added.11}
2. Click Sync Now in the top right of the Android Studio file editor window.
3. In your project's manifests folder, add the following permissions to AndroidManifest.xml: 1<manifest>2 ...existing content...3 4 <uses-permission android:name="android.permission.INTERNET" />5 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />6</manifest>

#### C#

For the latest version, refer to the [C# SDK documentation](https://www.pubnub.com/docs/sdks/c-sharp).

To install the many flavors of the C# SDK, refer to the following links for different package managers:

**C# 3.5/4.0/4.5/4.61**: [https://www.nuget.org/packages/Pubnub/4.6.0.0](https://www.nuget.org/packages/Pubnub/4.6.0.0)

**Universal Windows**: [https://www.nuget.org/packages/PubnubUWP/4.6.0.0](https://www.nuget.org/packages/PubnubUWP/4.6.0.0)

**Xamarin.Android, Xamarin.iOS, and .NET Core/.NET Standard**: [https://www.nuget.org/packages/PubnubPCL/4.6.0.0](https://www.nuget.org/packages/PubnubPCL/4.6.0.0)

#### Python

The simplest way to install the PubNub Python SDK is with `pip`:

```bash
pip install pubnub
```

### Initialize a PubNub object

Now set up a PubNub object with your publish and subscribe keys and a User ID. If you need keys, create an account in the [Admin Portal](https://admin.pubnub.com/#/signup). Use these keys in samples that show `myPublishKey` and `mySubscribeKey`. In code, parameter names can differ by SDK (for example, `userId`, `user_id`, or `uuid`). They map to the same User ID.

You don't need a new instance for every message. PubNub keeps TCP connections alive between calls (publish, subscribe, here-now).

#### JavaScript

```javascript
<script type="text/javascript">
  var pubnub = new PubNub({
    publishKey: "myPublishKey",
    subscribeKey: "mySubscribeKey",
    userId: "myUniqueUserId"
});
<script>
```

#### Python

```python
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub

pnconfig = PNConfiguration()
pnconfig.publish_key = "myPublishKey"
pnconfig.subscribe_key = "mySubscribeKey"
pnconfig.user_id = "myUniqueUserId"

pubnub = PubNub(pnconfig)
```

#### Java

```java
PNConfiguration.Builder configBuilder = PNConfiguration.builder(new UserId("yourUserId"), "yourSubscribeKey");
// publishKey from Admin Portal (only required if publishing)
configBuilder.publishKey("PublishKey");
PubNub pubNub = PubNub.create(configBuilder.build());
```

#### Kotlin

```kotlin
val config = PNConfiguration(UserId("myUniqueUserId")).apply {
      publishKey = "myPublishKey"
      subscribeKey = "mySubscribeKey"
  }
  var pubnub = PubNub(config)
```

#### Go

```go
pnconfig := pubnub.NewConfig()
pnconfig.SubscribeKey = "MySubscribeKey"
pnconfig.PublishKey = "MyPublishKey"
pnconfig.SetUserId(UserId("myUniqueUserId"))
pn := pubnub.NewPubNub(pnconfig)
```

#### C

```c
pubnub_t *ctx = pubnub_alloc();
if (NULL == ctx) {
    puts("Couldn't allocate a Pubnub context");
    return -1;
}
pubnub_init(ctx, "MyPublishKey", "MySubscribeKey");
pubnub_set_user_id(ctx, "myUniqueUser_id");
```

#### Unity

```csharp
using PubNubAPI;

PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.SubscribeKey = "my_subkey";
pnConfiguration.PublishKey = "my_pubkey";
pnConfiguration.UserId = "myUniqueUserId";
pubnub = new PubNub(pnConfiguration);
```

#### Swift

```swift
import PubNubSDK

let config = PubNubConfiguration(
  publishKey: "demo",
  subscribeKey: "demo",
  userId: "myUniqueUserId"
)

let pubnub = PubNub(configuration: config)
```

#### Objective-C

```objectivec
#import <PubNub/PubNub.h>

PNConfiguration *pnconfig = [PNConfiguration configurationWithPublishKey:@"myPublishKey"
                                             subscribeKey:@"mySubscribeKey"];
pnconfig.uuid = "theClientUUID";
PubNub *pubnub = [PubNub clientWithConfiguration:pnconfig];
```

#### C#

```csharp
using PubnubApi;

PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.SubscribeKey = "my_subkey";
pnConfiguration.PublishKey = "my_pubkey";
pnConfiguration.UserId = "myUniqueUserId";
pubnub = new PubNub(pnConfiguration);
```

#### Dart

```dart
final myKeyset = Keyset(
  subscribeKey: 'mySubscribeKey',
  publishKey: 'myPublishKey',
  userId: UserId('yourUniqueUserId')
  );
```

#### PHP

```php
use PubNub\PNConfiguration;
use PubNub\PubNub;

$pnconf = new PNConfiguration();

$pnconf->setSubscribeKey("my-key");
$pnconf->setPublishKey("my-key");
$pnconf->setUserId("myUniqueUserId");
$pubnub = new PubNub($pnconf);
```

#### Ruby

```ruby
pubnub = Pubnub.new(
    subscribe_key: :demo,
    publish_key:   :demo,
    user_id: 'myUniqueUserId'
)
```

At this point, you can start calling PubNub operations using the `pubnub` object.

#### Initialize with secret key

If you plan to use features such as [Access Manager](https://www.pubnub.com/docs/general/security/access-control), deleting messages from history, or [Functions](https://www.pubnub.com/docs/serverless/functions/overview), you must initialize PubNub with a secret key.

Use the secret key only on secure servers. Never expose it to client devices.

##### Node.js

```javascript
const pubnub = new PubNub({
   subscribeKey: 'mySubscribeKey',
   publishKey: 'myPublishKey',
   userId: "myUniqueUserId",
   secretKey: 'mySecretKey'
});
```

##### Python

```python
pn_config = PNConfiguration()
pn_config.publish_key = "my_publish_key"
pn_config.subscribe_key = "my_subscribe_key"
pn_config.user_id = "my_unique_user_id"
pn_config.secret_key = "my_secret_key"

pubnub = PubNub(pn_config)
```

##### Java

```java
PNConfiguration.Builder configBuilder = PNConfiguration.builder(new UserId("yourUserId"), "yourSubscribeKey");
// publishKey from Admin Portal (only required if publishing)
configBuilder.publishKey("PublishKey");
configBuilder.secretKey("mySecretKey");
PubNub pubNub = PubNub.create(configBuilder.build());
```

##### Kotlin

```kotlin
val config = PNConfiguration(UserId("myUniqueUserId")).apply {
    subscribeKey = "mySubscribeKey"
    publishKey = "myPublishKey"
    secretKey = "mySecretKey"
    secure = true
}
val pubnub = PubNub.create(config)
```

##### Go

```go
pnconfig := pubnub.NewConfig()
pnconfig.SubscribeKey = "MySubscribeKey"
pnconfig.PublishKey = "MyPublishKey"
pnconfig.SecretKey = "MySecretKey"
pnconfig.SetUserId(UserId("myUniqueUserId"))
pn := pubnub.NewPubNub(pnconfig)
```

##### C

```c
pubnub_init(ctx, "mySubscribeKey", "myPublishKey");
pubnub_set_secret_key(ctx, "mySecretKey");
pubnub_set_user_id(ctx, "myUniqueUser_id");
```

##### Unity

```csharp
using PubNubAPI;

PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.SubscribeKey = "my_subkey";
pnConfiguration.PublishKey = "my_pubkey";
pnConfiguration.SecretKey = "my_secretkey";
pnConfiguration.UserId = "myUniqueUserId";
pubnub = new PubNub(pnConfiguration);
```

##### C#

```csharp
using PubNubAPI;

PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.SubscribeKey = "my_subkey";
pnConfiguration.PublishKey = "my_pubkey";
pnConfiguration.SecretKey = "my_secretkey";
pnConfiguration.UserId = "myUniqueUserId";
pubnub = new PubNub(pnConfiguration);
```

##### Dart

```dart
final myKeyset = Keyset(
  subscribeKey: 'mySubscribeKey',
  publishKey: 'myPublishKey',
  secretKey: 'mySecretKey',
  userId: UserId('yourUniqueUserId')
  );
```

##### PHP

```php
use PubNub\PNConfiguration;
$pnConfiguration = new PNConfiguration();
$pnConfiguration->setSubscribeKey("MySubscribeKey");
$pnConfiguration->setPublishKey("MyPublishKey");
$pnConfiguration->setSecretKey("MySecretKey");
$pnConfiguration->setUserId("myUniqueUserId");
```

##### Ruby

```ruby
pubnub = Pubnub.new(
    subscribe_key: 'my_subscribe_key',
    publish_key: 'my_publish_key',
    secret_key: 'my_secret_key',
    user_id: 'myUniqueUserId'
)
```

For message deletion, see your [SDK documentation](https://www.pubnub.com/docs/sdks).

## Architectural decisions

For a simple app, start with the [architecture decisions](https://www.pubnub.com/docs/general/resources/architecture) page. For advanced apps that aggregate messages on the server, see the [server message aggregation guide](https://www.pubnub.com/docs/general/resources/design-pattern-message-aggregation).

* Use channels to route messages. Review [channel naming conventions](https://www.pubnub.com/docs/general/channels/channel-naming).
* Building a social app? See [friend lists and status feeds](https://www.pubnub.com/docs/general/resources/design-pattern-friend-list-status-feeds).

## Terms in this document

* **Publish Key** - A unique identifier that allows your application to send messages to PubNub channels. It's part of your app's credentials and should be kept secure.
* **PubNub** - PubNub is a real-time messaging platform that provides APIs and SDKs for building scalable applications. It handles the complex infrastructure of real-time communication, including: Message delivery and persistence, Presence detection, Access control, Push notifications, File sharing, Serverless processing with Functions and Events & Actions, Analytics and monitoring with BizOps Workspace, AI-powered insights with Illuminate.
* **Subscribe Key** - A unique identifier that allows your application to receive messages from PubNub channels. It's part of your app's credentials and should be kept secure.
* **User** - An individual or entity that interacts with a system, application, or service. In PubNub, a user typically refers to someone who sends or receives messages through the platform, identified by a unique user ID or username.
* **User ID** - UTF-8 encoded, unique string of up to 92 characters used to identify a single client (end user, device, or server) that connects to PubNub.
