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 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 or whether to encrypt messages. 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, depending on the features you want to use.

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:

var pubnub = new PubNub({
publishKey: "myPublishKey",
subscribeKey: "mySubscribeKey",
userId: "myUniqueUserId"
});
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.

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.

Last updated on
On this page