Configuration API for JavaScript SDK
JavaScript complete API reference for building real-time applications on PubNub, including basic usage and sample code.
You must include the PubNub JavaScript SDK in your code before initializing the client.
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.9.5.1.js"></script>
Bundling
PubNub JavaScript SDK allows you to opt out of modules you don't want to use in your application. You can use either Rollup or Webpack to bundle modules.
We provide a configuration file you can use to create your bundle configuration for both systems.
- Rollup
- Webpack
-
Clone the PubNub JavaScript SDK repository to a directory of your choice.
-
In the
rollup.config.js
Rollup configuration file, change the value of theenableTreeShaking
constant totrue
. -
Inspect the
rollup.config.js
file and decide which modules you want to bundle. Disable the unnecessary modules by setting the environment variables with their labels todisabled
, for example, in Terminal:export PRESENCE_MODULE=disabled PUBLISH_MODULE=disabled
-
Run the
rollup -c rollup.config.js --bundleConfigAsCjs
command to bundle the modules. Once the process is finished, you can find the bundle in the your_js_sdk_repo_dir/upload
directory.
-
Clone the PubNub JavaScript SDK repository to a directory of your choice.
-
In the repository, create a
webpack.config.js
file and base its contents on the Rollup configuration file. This setup allows you to dynamically change which modules are enabled/disabled by setting environment variables in Terminal.
show all 24 linesconst webpack = require('webpack');
module.exports = {
// Other configuration...
plugins: [
new webpack.DefinePlugin({
'process.env.CRYPTO_MODULE': JSON.stringify(process.env.CRYPTO_MODULE ?? 'enabled'),
'process.env.SHARED_WORKER': JSON.stringify(process.env.SHARED_WORKER ?? 'enabled'),
'process.env.PUBLISH_MODULE': JSON.stringify(process.env.PUBLISH_MODULE ?? 'enabled'),
'process.env.SUBSCRIBE_MODULE': JSON.stringify(process.env.SUBSCRIBE_MODULE ?? 'enabled'),
'process.env.SUBSCRIBE_EVENT_ENGINE_MODULE': JSON.stringify(process.env.SUBSCRIBE_EVENT_ENGINE_MODULE ?? 'enabled'),
'process.env.SUBSCRIBE_MANAGER_MODULE': JSON.stringify(process.env.SUBSCRIBE_MANAGER_MODULE ?? 'enabled'),
'process.env.PRESENCE_MODULE': JSON.stringify(process.env.PRESENCE_MODULE ?? 'enabled'),
'process.env.PAM_MODULE': JSON.stringify(process.env.PAM_MODULE ?? 'enabled'),
'process.env.CHANNEL_GROUPS_MODULE': JSON.stringify(process.env.CHANNEL_GROUPS_MODULE ?? 'enabled'),Webpack configuration
Make sure to add any other necessary configuration to the
webpack.config.js
. For more information, refer to Webpack documentation. -
Disable the unnecessary modules by setting the environment variables with their labels to
disabled
, for example, in Terminal:export PRESENCE_MODULE=disabled PUBLISH_MODULE=disabled
-
Run the
npx webpack --config webpack.config.js
command to bundle the modules. Once the process is finished, you can find the bundle in the directory you configured in thewebpack.config.js
file.
Initialization
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 Admin Portal.
Method(s)
To Initialize
PubNub, you can use the following method(s) in the JavaScript SDK:
pubnub.PubNub({
subscribeKey: string,
publishKey: string,
userId: string,
authKey: string,
logVerbosity: boolean,
ssl: boolean,
origin: string | string[],
presenceTimeout: number,
heartbeatInterval: number,
keepAlive: boolean,
keepAliveSettings: any,
suppressLeaveEvents: boolean,
requestMessageCountThreshold: number,
enableEventEngine: boolean
show all 25 linesParameter | Description |
---|---|
subscribeKey *Type: string Default: n/a | Specifies the subscribeKey to be used for subscribing to a channel. This key can be specified at initialization or along with a subscribe() . |
publishKey Type: string Default: n/a | Specifies the publishKey to be used for publishing messages to a channel. This key can be specified at initialization or along with a publish() . |
userId *Type: string Default: n/a | userId to use. You should set a unique userId to identify the user or the device that connects to PubNub.It's a UTF-8 encoded string of up to 92 alphanumeric characters. If you don't set theuserId , you won't be able to connect to PubNub. |
secretKey Type: string Default: n/a | Secret key from Admin Portal. When you initialize PubNub with secretKey , you get root permissions for Access Manager. For more information, refer to Server Operations. |
authKey Type: string Default: n/a | The token returned from Access Manager that provides access to resources. For more information, refer to Access Manager. |
logVerbosity Type: boolean Default: false | Log HTTP information. |
ssl Type: boolean Default: true for v4.20.0 onwards, false before v4.20.0 | If set to true , requests will be made over HTTPS. |
origin Type: string or string[] Default: ps.pndsn.com | If a custom domain is required, SDK accepts it here. |
presenceTimeout Type: number Default: 300 | Defines how long the server considers the client alive for presence. This property works similarly to the concept of long polling by sending periodic requests to the PubNub server every 300 seconds by default. These requests ensure the client remains active on subscribed channels. If no heartbeat is received within the timeout period, the client is marked inactive, triggering a "timeout" event on the presence channel. |
heartbeatInterval Type: number Default: Not Set | Specifies how often the client will send heartbeat signals to the server. This property offers more granular control over client activity tracking than presenceTimeout . Configure this property to achieve a shorter presence timeout if needed, with the interval typically recommended to be (presenceTimeout / 2) - 1 . |
keepAlive Type: boolean Default: false | 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. |
keepAliveSettings Type: any Default: 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. |
suppressLeaveEvents Type: boolean Default: false | When true the SDK doesn't send out the leave requests. |
requestMessageCountThreshold Type: number Default: 100 | PNRequestMessageCountExceededCategory is thrown when the number of messages into the payload is above of requestMessageCountThreshold . |
enableEventEngine Type: boolean Default: false | Whether to use the recommended standardized workflows for subscribe and presence, optimizing how the SDK internally handles these operations and which statuses it emits. Refer to SDK connection lifecycle for more information. This flag affects the following parameters:
|
restore Type: boolean Default: false | This option is available only in the browser environment and requires listenToBrowserNetworkEvents to be set to true . A flag to allow catch up on the front-end applications. Its main purpose is to manage the state in which the SDK will be set after the network goes down:
|
retryConfiguration Type: RequestRetryPolicy Default: PubNub.ExponentialRetryPolicy (subscribe only) | This option is available in the browser and Node.js environments. Custom reconnection configuration parameters. You can specify one or more endpoint groups for which the retry policy won't be applied. retryConfiguration: policy is the type of policy to be used. Available values:
excluded takes an array of Endpoint enum values, for example, excluded: [PubNub.Endpoint.MessageSend] .For more information, refer to SDK connection lifecycle. |
autoNetworkDetection Type: boolean Default: false | This option is available in the browser and Node.js environments, only when enableEventEngine is false .Whether the SDK should emit the PNNetworkDownCategory and PNNetworkUpCategory statuses on network status change. |
listenToBrowserNetworkEvents Type: boolean Default: true | This option is available only in the browser environment. Whether the SDK should emit the PNNetworkDownCategory and PNNetworkUpCategory statuses, listen for the browser reachability events, and try to reconnect on network status change.If the browser fails to detect the network changes from WiFi to LAN or you get reconnection issues, set the flag to false . This allows the SDK reconnection logic to take over. |
maintainPresenceState Type: boolean Default: true | Works only when enableEventEngine is true . Whether the custom presence state information set using pubnub.setState() should be sent every time the SDK sends a subscribe call. |
cryptoModule Type: PubNub.CryptoModule.legacyCryptoModule({ cipherKey, useRandomIVs }) PubNub.CryptoModule.aesCbcCryptoModule({cipherKey}) Default: None | The cryptography module used for encryption and decryption of messages and files. Takes the cipherKey and useRandomIVs parameters as arguments. For more information, refer to the cryptoModule section. |
subscriptionWorkerUrl Type: string Default: None | The URL for the shared worker on the server or the origin as the page where it's used. For more information, refer to Shared workers. |
cipherKey Type: Default: n/a | This way of setting this parameter is deprecated, pass it to cryptoModule instead. |
useRandomIVs Type: Default: true | This way of setting this parameter is deprecated, pass it to cryptoModule instead. true the initialization vector (IV) is random for all requests (not just for file upload). When false the IV is hard-coded for all requests except for file upload. |
uuid Type: Default: n/a | This parameter is deprecated, use userId instead.UUID to use. You should set a unique UUID to identify the user or the device that connects to PubNub. If you don't set the UUID , you won't be able to connect to PubNub. |
Disabling random initialization vector
Disable random initialization vector (IV) only for backward compatibility (<4.31.0
) with existing applications. Never disable random IV on new applications.
cryptoModule
cryptoModule
provides encrypt/decrypt functionality for messages and files. From the 7.3.3 release on, you can configure how the actual encryption/decryption algorithms work.
Each PubNub SDK is bundled with two ways of encryption: the legacy encryption with 128-bit cipher key entropy and the recommended 256-bit AES-CBC encryption. For more general information on how encryption works, refer to the Message Encryption and File Encryption sections.
If you do not explicitly set the cryptoModule
in your app and have the cipherKey
and useRandomIVs
params set in PubNub config, the client defaults to using the legacy encryption.
Legacy encryption with 128-bit cipher key entropy
You don't have to change your encryption configuration if you want to keep using the legacy encryption. If you want to use the recommended 256-bit AES-CBC encryption, you must explicitly set that in PubNub config.
cryptoModule
configuration
To configure the cryptoModule
to encrypt all messages/files, you can use the following methods in the Javascript SDK:
// encrypts using 256-bit AES-CBC cipher (recommended)
// decrypts data encrypted with the legacy and the 256 bit AES-CBC ciphers
const pn = new PubNub({
cryptoModule: PubNub.CryptoModule.aesCbcCryptoModule({cipherKey: 'pubnubenigma'});
});
// encrypts with 128-bit cipher key entropy (legacy)
// decrypts data encrypted with the legacy and the 256-bit AES-CBC ciphers
const pn = new PubNub({
cryptoModule: PubNub.CryptoModule.legacyCryptoModule({cipherKey: 'pubnubenigma', useRandomIVs: true});
});
Your client can decrypt content encrypted using either of the modules. This way, you can interact with historical messages or messages sent from older clients while encoding new messages using the more secure 256-bit AES-CBC cipher.
Older SDK versions
Apps built using the SDK versions lower than 7.3.3 will not be able to decrypt data encrypted using the 256-bit AES-CBC cipher. Make sure to update your clients or encrypt data using the legacy algorithm.
Shared workers
Shared workers manage concurrent connections and maintain presence states across multiple client instances. They're useful in scenarios where you have multiple browser tabs or windows using the PubNub SDK. PubNub's shared worker source code is accessible via our CDN:
http://cdn.pubnub.com/sdk/javascript/pubnub.worker.9.5.1.js
There are two main reasons for implementing shared workers:
-
Browser per-origin parallel connections limit
Browsers limit the number of parallel connections that can be established to the same origin (e.g.,
ps.pndsn.com
). Due to this limitation, having multiple PubNub clients operating independently in the same context may produce connection issues. Shared workers can aggregate subscriptions for matching keysets, allowing them to utilize one long-poll subscribe request rather than one per client. -
Presence maintenance with the "Generate Leave on TCP FIN or RST" feature
This feature emits a
leave
presence event immediately when a connection used for a long-poll subscribe request is closed, which can lead to false presence events if multiple tabs or windows are open. The shared worker tracks channels and groups to ensure long-poll requests are only truly terminated when all associated tabs or windows are closed, preventing incorrect presence updates.
Hosting a shared worked
As browsers enforce the Same-origin Policy, you must host the shared worker under the same origin as the client application. Pages loaded from different origins won't be able to share a worker.
Configuration
-
Download the shared worker source code from our CDN.
http://cdn.pubnub.com/sdk/javascript/pubnub.worker.9.5.1.js
-
Host the shared worker on your server or make it available under the same origin as the page where it's used according to the Same-origin Policy.
-
Configure the
subscriptionWorkerUrl
property when initializing the PubNub client:
const pubnub = new PubNub({
subscribeKey: "demo",
publishKey: "demo",
userId: "unique-user-id",
// using PubNub JS SDK v9.5.1, make sure the versions match
subscriptionWorkerUrl: 'https://www.my-domain.com/static/js/pubnub.worker.9.5.1.js'
});
When this URL is specified, the PubNub client downloads the shared worker's source code and uses it to manage subscriptions.
Worker version
The version of the shared worker must match the version of the PubNub client being used.
Basic Usage
Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.
Applications can initialize
the PubNub object by passing the subscribeKey
and publishKey
keys from your account. Each client should also pass a userId
that represents the user or the device that connects to PubNub.
Required UserId
Always set the userId
to uniquely identify the user or device that connects to PubNub. This userId
should be persisted and should remain unchanged for the lifetime of the user or the device. If you don't set the userId
, you won't be able to connect to PubNub.
const PubNub = require('pubnub');
// Initialize PubNub with demo keys
const pubnub = new PubNub({
subscribeKey: 'demo',
publishKey: 'demo',
userId: 'myUniqueUserId',
cryptoModule: PubNub.CryptoModule.aesCbcCryptoModule({ cipherKey: 'pubnubenigma' }),
authKey: 'myAccessManagerToken',
logVerbosity: true,
ssl: true,
presenceTimeout: 130
});
// Log initialization details
show all 16 linesServer Operations
For servers connecting to PubNub, setting a userId
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 Access Manager 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 to all channels and channel groups.
Secure your secretKey
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
applications or other server-side platforms.
Required UserId
Always set the userId
to uniquely identify the user or device that connects to PubNub. This userId
should be persisted and should remain unchanged for the lifetime of the user or the device. If you don't set the userId
, you won't be able to connect to PubNub.
var pubnub = new PubNub({
subscribeKey: "mySubscribeKey",
publishKey: "myPublishKey",
userId: "myUniqueUserId",
secretKey: "secretKey",
heartbeatInterval: 0
});
Now that the pubnub object is instantiated, the client will be able to access the Access Manager functions. The pubnub object will use the secretKey
to sign all Access Manager messages to the PubNub Network.