---
source_url: https://www.pubnub.com/docs/general/basics/initialize-pubnub
title: Initialize PubNub
updated_at: 2026-05-25T11:26:05.313Z
---

> 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


# Initialize PubNub

Before you start calling PubNub APIs, you must initialize the PubNub object in your application. This step uses the publish and subscribe keys from the [Admin Portal](https://admin.pubnub.com/) and enables PubNub functionality. You can customize how PubNub works by setting different options. These options control security, performance, and features—for example, how to handle a [connection interruption](https://www.pubnub.com/docs/general/setup/connection-management) or whether to [encrypt messages](https://www.pubnub.com/docs/general/setup/data-security). Transport encryption (SSL/TLS) is enabled by default.

You only need to provide `subscribeKey` and a User ID to receive messages. If you plan to send messages, you must also configure `publishKey`. There are other [ways of initializing PubNub](https://www.pubnub.com/docs/general/setup/application-setup#initialize-a-pubnub-object), depending on the features you want to use.

:::note Make User ID descriptive
It's up to you to define a `userId`, which is a UTF-8 encoded string of up to 92 characters used to identify the device that connects to PubNub.
:::

Check out how easy it is:

###### JavaScript

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

###### Node.js

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

###### Swift

```swift
import PubNubSDK

var pnconfig = PubNubConfiguration(
  publishKey: "myPublishKey",
  subscribeKey: "mySubscribeKey"
)

pnconfig.userId = "myUniqueUserId"
let pubnub = PubNub(configuration: pnconfig)
```

###### Objective-C

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

###### Java

```java
PNConfiguration.Builder configBuilder = PNConfiguration.builder(new UserId("yourUserId"), "yourSubscribeKey");
configBuilder.publishKey("myPublishKey");
PubNub pubNub = PubNub.create(configBuilder.build());
```

###### C#

```csharp
PNConfiguration pnconfig = new PNConfiguration();
pnconfig.PublishKey = "myPublishKey";
pnconfig.SubscribeKey = "mySubscribeKey";
pnconfig.UserId = "myUniqueUserId";

Pubnub pubnub = new Pubnub(pnconfig);
```

###### 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)
```

###### Dart

```dart
var pubnub = PubNub(
      defaultKeyset:
          Keyset(subscribeKey: 'mySubscribeKey', publishKey: 'myPublishKey', userId: UserId('myUniqueUserId')));
```

###### Kotlin

```kotlin
val builder = PNConfiguration.builder(UserId("myUniquesUserID"), "subscribeKey") {
    publishKey = "publishKey"
}
val pubnub = PubNub.create(builder.build())
```

:::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).
:::

You don't have to create a separate PubNub object every time you want to send or receive a message. After the initialization, `subscribeKey`, `publishKey`, and `userId` are reused for all operations.

Let's look into [the userId parameter a bit more](https://www.pubnub.com/docs/general/basics/identify-users-and-devices).

## 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.
* **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.
