Application Setup

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

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.

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

// 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`

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

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

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, deleting messages from history, or Functions, you must initialize PubNub with a secret key.

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

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

For message deletion, see your SDK documentation.

Architectural decisions

For a simple app, start with the architecture decisions page. For advanced apps that aggregate messages on the server, see the server message aggregation guide.

Last updated on