JavaScriptNode.jsNode.jsPhoneGapReactTitaniumVueWebNode.js V4 Configuration API Reference for Realtime Apps

Node.js V4 complete API reference for building Realtime Applications on PubNub, including basic usage and sample code.

You must include the PubNub Node.js V4 SDK in your code before initializing the client.

<script src=""></script>

Use this method to initialize the PubNub Client API context and establish account-level credentials such as publish and subscribe keys. You can create an account and get your keys from the PubNub Dashboard.

To Initialize Pubnub you can use the following method(s) in the Node.js V4 SDK:

  1. ParameterTypeRequiredDefaultsDescription
    Operation Arguments Hash YesA hash of arguments.
    subscribeKey StringYesSpecifies the subscribeKey to be used for subscribing to a channel. This key can be specified at initialization or along with a subscribe().
    publishKey String OptionalSpecifies the publishKey to be used for publishing messages to a channel. This key can be specified at initialization or along with a publish().
    secretKey String OptionalUsed for PAM operations (server only!!).
    cipherKey String OptionalIf passed, will encrypt the payloads.
    authKey StringOptionalIf PAM enabled, this key will be used on all requests.
    logVerbosity Boolean Optionalfalselog HTTP information.
    uuid String YesSDK generated random uuid

    UUID to use. You should set a unique UUID to identify the user or the device that connects to PubNub.

    Allowing the SDK to generate a random UUID can result in significant billing impacts, particularly on an MAU pricing plan.

    ssl Boolean Optional

    true for v4.20.0 onwards,

    false before v4.20.0

    If set to true, requests will be made over HTTPS.
    origin String Optionalps.pndsn.comIf a custom domain is required, SDK accepts it here.
    presenceTimeout Number Optional300

    How long the server will consider the client alive for presence.

    The value is in seconds.

    heartbeatInterval Number OptionalNot Set

    How often the client will announce itself to server.

    The value is in seconds.

    restore Boolean Optionalfalsetrue to allow catch up on the front-end applications.

    true for v4.18.0 onwards,

    false before v4.18.0

    If set to true, SDK will use the same TCP connection for each HTTP request, instead of opening a new one for each new request.

    keepAliveMsecs: 1000

    freeSocketKeepAliveTimeout: 15000

    timeout: 30000

    maxSockets: Infinity

    maxFreeSockets: 256

    Set a custom parameters for setting your connection keepAlive if this is set to true.

    keepAliveMsecs: (Number) how often to send TCP KeepAlive packets over sockets.

    freeSocketKeepAliveTimeout: (Number) sets the free socket to timeout after freeSocketKeepAliveTimeout milliseconds of inactivity on the free socket.

    timeout: (Number) sets the working socket to timeout after timeout milliseconds of inactivity on the working socket.

    maxSockets: (Number) maximum number of sockets to allow per host.

    maxFreeSockets: (Number) maximum number of sockets to leave open in a free state.

    useInstanceId Boolean Optional InstanceId is created for each new instance done and this is a composition of the prefix pn- and an uuid v4, this instanceId is used for signing each request to the service.
    suppressLeaveEvents Boolean OptionalfalseWhen true the SDK doesn't send out the leave requests.

    Instruct the SDK to use a proxy configuration when communicating with PubNub servers.

    Supported Protocols are http, https, socks5, socks4 and pac.


    PNRequestMessageCountExceededCategory is thrown when the number of messages into the payload is above of requestMessageCountThreshold.

    listenToBrowserNetworkEventsBooleanOptionalfalseIf the browser fails to detect the network changes from WiFi to LAN and vice versa or you get reconnection issues, set the flag to false. This allows the SDK reconnection logic to take over.

Applications can initialize the PubNub object by passing the subscribeKey and publishKey keys from your account. Each client should also pass a UUID that represents the user or the device that connects to PubNub.

 Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
// PubNub = require('pubnub'); 		ES5
import PubNub from 'pubnub';		ES6

var pubnub = new PubNub({
	subscribeKey: "mySubscribeKey",
	publishKey: "myPublishKey",
	secretKey: "secretKey",
	cipherKey: "myCipherKey",
	authKey: "myAuthKey",
	logVerbosity: true,
	uuid: "myUniqueUUID",
	ssl: true,
	presenceTimeout: 130

For servers connecting to PubNub, setting a UUID is different than for a client device as there can be multiple instances of the server on the same machine and there is no "authentication" process for a server (at least not like an end user).

The API can be initialized with the secretKey if the server needs to administer PAM permissions for client applications. When you initialize PubNub with secretKey, you get root permissions for the Access Manager. With this feature you don't have to grant access to your servers to access channels or channel groups. The servers get all access on all channels and channel groups.


Anyone with the secretKey can grant and revoke permissions to your app. Never let your secretKey be discovered, and only exchange and deliver it securely. Only use the secretKey on secure environments such as Node.js application or other server-side platforms.

var pubnub = new PubNub({
    subscribeKey: "mySubscribeKey",
    publishKey: "myPublishKey",
    uuid: "myUniqueUUID",
    secretKey: "secretKey",
    heartbeatInterval: 0

Now that the pubnub object is instantiated, the client will be able to access the PAM functions. The pubnub object will use the secretKey to sign all PAM messages to the PubNub Network.

  1. In the case where a client will only read messages and never publish to a channel, you can simply omit the publishKey when initializing the client:
    // Initialize for Read Only Client
    var pubnub = new PubNub({
        subscribeKey: "mySubscribeKey",
        Uuid: "myUniqueUUID"
  2. This examples demonstrates how to enable PubNub Transport Layer Encryption with TLS(formerly known as SSL). Just initialize the client with ssl set to true. The hard work is done, now the PubNub API takes care of the rest. Just subscribe and publish as usual and you are good to go.

    var pubnub = new PubNub({
        subscribeKey: "mySubscribeKey",
        publishKey: "myPublishKey",
        cipherKey: "myCipherKey",
        authKey: "myAuthKey",
        logVerbosity: true, // set false for production
        uuid: "myUniqueUUID",
        ssl: true

A UUID (Universal Unique Identifier) is a unique alphanumeric identifier used to identify the client to the PubNub platform. Each client should pass a UUID that represents the user or the device that connects to PubNub.

Set the UUID parameter when you instantiate a PubNub instance (i.e., new PubNub()). It is important that your application reuse the UUID on each device instead of generating a new UUID on each connection.

var pubnub = new PubNub({
    subscribeKey: "mySubscribeKey",
    publishKey: "myPublishKey",
    uuid: "myUniqueUUID"
You can also call the following method to explicitly set the UUID:
  1. ParameterTypeRequiredDescription
    uuid String YesUUID to set.

Providing a value for the uuid parameter in the PubNub object initialization will result in that value getting saved in the browser's localStorage key (described above) automatically by the PubNub SDK. You may implement a different local caching strategy, as required.

Consider the following when implementing a UUID reuse strategy:

  1. On your server, generate a UUID the when a user creates a user profile (user registration process). You can generate this with the SDK, or by another method of your choosing.
  2. Pass the UUID back to the user upon successful login (authentication process).
  3. Persist the UUID on the device where it can be retrieved the next time the PubNub instance is instantiated. The PubNub instance might be instantiated multiple times as the app is left and re-entered, and you may not require a login with each new session.

Use this method to get the current UUID set on your application.This method doesn't take any arguments.


If you have not explicitly set a UUID, this method returns a generated UUID of the form "pn-...".

Each PubNub SDK provides a UUID generator API (for example, generateUUID). It is not required that you use this API to create a UUID and this format of the UUID is not required (any string will do up to 64 characters). Just consider that whatever you use will be visible to other users (if a user peeks behind the scenes using the browser console or other tools), so you should not use a username or email as the UUID. The UUID should be something that can be easily replaced as required without user interaction or even knowledge that it has happened.

If your application does not need to use the UUID parameter for presence operations and account is not billed based on Monthly Active Users (MAUs) based pricing model.

This function provides the capability to reset a user's auth Key.
Typically auth Key is specified during initialization for PubNub Access Manager enabled applications. In the event that auth Key has expired or a new auth Key is issued to the client from a Security Authority, the new auth Key can be sent using setAuthKey().
To Set Authentication Key you can use the following method(s) in the Node.js V4 SDK
  1. ParameterTypeRequiredDescription
    key String YesAuth key to set.

Requires Stream Controller add-on XRequires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:
To set/get filters you can use the following methods.
  1. ParameterTypeRequiredDescription
    filterExpressionStringYesPSV2 feature to subscribe with a custom filter expression.
  2. This method doesn't take any arguments.