Set up your JavaScript environment

This guide sets up a development environment for the PubNub JavaScript SDK (the pubnub package) and installs it. Do the sections that match your runtime and skip the rest. To pick a runtime first, refer to Choose your runtime.

Install the SDK​

Install the pubnub package with the package manager your project uses:

# npm (bundled with Node.js)
npm install pubnub

# yarn
yarn add pubnub

# pnpm
pnpm add pubnub

# bun
bun add pubnub

Each package manager writes its own lockfile: package-lock.json, yarn.lock, pnpm-lock.yaml, or bun.lockb. Commit the lockfile so installs stay the same across environments.

To build the SDK yourself, clone the GitHub repository:

1git clone https://github.com/pubnub/javascript

Set up TypeScript​

If you write TypeScript, you do not need an additional @types package, because the PubNub JavaScript SDK includes its type definitions.

  1. Install TypeScript as a dev dependency:

    npm install --save-dev typescript
  2. Create a tsconfig.json file:

    npx tsc --init
  3. Import PubNub with types:

    
    

For PubNub projects, use these tsconfig.json settings:

{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}

Choose a module format​

The PubNub JavaScript SDK supports both ES Modules (ESM) and CommonJS imports.

If your project uses ES Modules, import the SDK with import. The pubnub package works with ESM through Node.js interop and bundlers such as Vite, webpack, and esbuild:

1

If your project uses CommonJS, import the SDK with require:

const PubNub = require('pubnub');

To use ES Modules in Node.js, do one of the following:

  • Use the .mjs file extension.
  • Add "type": "module" to your package.json.
  • Use a bundler such as Vite or esbuild.

Load the SDK from a CDN​

If your browser application has no bundler, load the PubNub JavaScript SDK from the PubNub content delivery network (CDN):

<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.13.1.3.js"></script>

Pin the script to a specific version, such as pubnub.13.1.3.js, instead of latest. A pinned version cannot introduce breaking changes without you choosing to upgrade.

Share subscriptions across browser tabs​

Run a shared worker to manage subscriptions and presence for several PubNub JavaScript SDK clients in different browser tabs or windows. Shared workers work in browsers only. Use one if several tabs of your app reach the browser limit on parallel connections to one origin, for example ps.pndsn.com, or if closing a tab produces false leave events.

  1. Download the worker from the CDN. Its version must match the version of the PubNub client:

    https://cdn.pubnub.com/sdk/javascript/pubnub.worker.13.1.3.js
  2. Host the worker on your server under the same origin as the page that uses it. Browsers enforce the Same-origin Policy, so pages loaded from other origins cannot share a worker.

  3. Set subscriptionWorkerUrl to the URL of the hosted worker. The client downloads the worker and uses it to manage subscriptions:

    1
    

The worker aggregates subscriptions for matching keysets into one long-poll subscribe request. It tracks channels and channel groups and ends the request only when all associated tabs are closed.

Build a custom bundle​

The PubNub JavaScript SDK builds with Rollup or Webpack, and environment variables decide which modules the bundle contains. Setting a variable to disabled removes that module through tree-shaking.

VariableModuleDefault
CRYPTO_MODULECryptors, including Concise Binary Object Representation (CBOR) supportenabled
SHARED_WORKERBrowser background subscription requestsenabled
PUBLISH_MODULEMessages and signalsenabled
SUBSCRIBE_MODULEReal-time updates and their handlingenabled
SUBSCRIBE_EVENT_ENGINE_MODULESubscribe with the standardized workflows (enableEventEngine: true)enabled
SUBSCRIBE_MANAGER_MODULESubscribe with the legacy subscription manager (enableEventEngine: false)enabled
PRESENCE_MODULEPresence and user stateenabled
PAM_MODULEAccess Manager token and authKey permissionsdisabled
CHANNEL_GROUPS_MODULEChannel groupsenabled
MESSAGE_PERSISTENCE_MODULEMessage Persistence access and message removalenabled
MOBILE_PUSH_MODULEMobile push notifications for channelsenabled
APP_CONTEXT_MODULEApp Context entities and relationshipsenabled
DATA_SYNC_MODULEDataSync entities, relationships, and typed resourcesenabled
FILE_SHARING_MODULEFile sharingenabled
MESSAGE_REACTIONS_MODULEMessage reactions (message actions)enabled

The Rollup configuration disables PAM_MODULE by default, which is why browser builds lack Access Manager support. To keep Access Manager in a custom bundle, set PAM_MODULE=enabled. That bundle exposes the Access Manager code in the browser, so build it only for cases such as an internal admin tool.

Use the SDK's rollup.config.js as a starting point.

  1. Clone the PubNub JavaScript SDK repository.

  2. In rollup.config.js, set enableTreeShaking to true.

  3. Disable the modules you do not need, for example:

    export PRESENCE_MODULE=disabled PUBLISH_MODULE=disabled
  4. Run rollup -c rollup.config.js --bundleConfigAsCjs. The bundle is in the upload directory of your clone.

Set up React Native​

In a React Native project, install the SDK together with react-native-url-polyfill:

npm install pubnub react-native-url-polyfill

As of SDK v12.0.0, the SDK no longer bundles react-native-url-polyfill, so you must install it explicitly. After you install it, the SDK applies the polyfill automatically and needs no additional import or setup step.

Next steps​

Was this page useful?

Last updated on