Quickstart: PubNub JavaScript SDK in Node.js
This quickstart walks you through a "Hello, World" Node.js script that connects to PubNub, subscribes to a channel, and publishes a message that arrives in the same process. You need one file and a terminal.
Building for the browser, React, or React Native instead? Refer to Choose your runtime.
Before you begin
You need Node.js installed (version 22 or later) and a PubNub account. For package managers and TypeScript, refer to Set up your JavaScript environment.
Get your PubNub keys
First, get your PubNub keys:
- Sign in or create an account on the PubNub Admin Portal
- Create an app (or use an existing one)
- Find your publish and subscribe keys in the app dashboard
When you create an app, PubNub automatically generates a keyset. You can use the same keyset for development and production, but we recommend separate keysets for each environment to improve security and management.
Install
SDK version
Always use the latest SDK version to have access to the newest features and avoid security vulnerabilities, bugs, and performance issues.
To integrate PubNub into your Node.js project, create a project folder and install the SDK using npm:
1mkdir pubnub-quickstart
2cd pubnub-quickstart
3npm install pubnub
This quickstart uses an ES module, so you save the code in a file named index.mjs. That lets you use import and top-level await. If your project uses CommonJS, import the SDK with const PubNub = require('pubnub'); instead.
1
Initialize
Create an instance of the PubNub class with your keys and a unique user ID. Replace the placeholders with the publish and subscribe keys from the Admin Portal.
1
Always set the userId to uniquely identify the user or device that connects to PubNub. Keep it 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.
For more information, refer to the Configuration section of the SDK documentation.
Subscribe
Listeners help the application react to events and messages. This listener prints each incoming message and logs when the client connects.
1
To receive messages on a channel, you need to subscribe to it. PubNub offers an entity-based subscription approach that gives you more control and flexibility.
1
For complete details on subscribing and handling events, refer to the Subscribe section of the SDK documentation.
Publish
When you publish a message to a channel, PubNub delivers that message to everyone who is subscribed to that channel. A message can be any JSON-serializable data (such as objects, arrays, integers, strings) that is smaller than 32 KiB.
This code publishes once the client is connected, so the subscription is already active and your own listener receives the message. It adds a second status listener that runs when the client reports PNConnectedCategory.
1
For more information, refer to the Publish section of the SDK documentation.
Run
The file below combines all the code from the previous steps. Save it as index.mjs in your project folder and replace the three placeholders.
1
Run it with Node.js:
1node index.mjs
Verify that it works. The terminal prints these lines, and their order can vary:
Connected to PubNub
Published, timetoken: <a 17-digit timetoken>
Received on hello_world: Hello, world!
The script keeps running because the subscription stays open. Press Ctrl+C to stop it. To see real-time communication between two clients, run the script in a second terminal while the first one is still running. The first terminal prints another Received on hello_world: Hello, world! line.
If the message does not arrive, refer to Troubleshooting. If you see a 403 (Access Denied) error, your keyset may have Access Manager enabled. Verify that your token has read permissions for the subscribed channel. Refer to Configure access control.
Next steps
You created your first PubNub JavaScript application. Here is what to explore next:
- Build a feature with the how-tos, such as showing who is online or loading message history.
- Learn about the JavaScript Chat SDK for ready-to-use chat features such as typing indicators and read receipts.
- Try out Presence to track online and offline status.
- Implement Message Persistence to store and retrieve messages.
- Use Access Manager to secure your channels.
- Explore Channel Groups to organize your channels.
- Explore the GitHub repository for more code samples.
- Follow a tutorial: IoT dashboard, Geolocation app, or Delivery app.
- Check the SDK reference documentation for detailed API information.
- Visit the support portal or ask the AI assistant (the looking glass icon at the top of the page).