Build a JavaScript chat app with PubNub
Build a browser chat with the PubNub JavaScript SDK: real-time messages, who's online, message history, display names and timestamps.
Set up your environment
In this tutorial you build a chat that runs in the browser, using the PubNub JavaScript SDK. It's plain HTML and JavaScript, with no build step and no server. You need:
- A current browser: Chrome, Firefox, Safari or Edge
- A code editor, for example Visual Studio Code
- Git
Get the code
Clone the repository and open its folder:
git clone https://github.com/PubNubDevelopers/Javascript-Chat-Tutorial-App.git
cd Javascript-Chat-Tutorial-AppThe repository has one folder per step of the build, from Step 1 - Chat Layout to Step 6 - Message Timestamps. Open each step's folder as you go, starting with Step 1 - Chat Layout/index.html. Each build step explains the key parts of the code; the step's folder has the complete file to compare with or run.
Tested with the PubNub JavaScript SDK 13.1.3 (October 2026).
Create keys
You need a free PubNub account and a keyset with Presence and Message Persistence. Create it with your AI coding tool and the PubNub MCP Server, or with the Admin API.
With the PubNub MCP Server
The PubNub MCP Server lets your AI tool create apps and keysets in your account. It runs at https://mcp.pubnub.com and you sign in with your PubNub account, so there's nothing to install. MCP connections must be allowed for your organization; an Account Owner or Account Admin controls this in organization settings.
Create a free account if you don't have one, then add the server to your tool:
- Claude Code: run
claude mcp add --scope user --transport http pubnub https://mcp.pubnub.com, startclaude, enter/mcp, select pubnub, then authenticate, and sign in. - Cursor: add
{"mcpServers": {"PubNub": {"url": "https://mcp.pubnub.com"}}}to.cursor/mcp.json, click Enable in the notification, and sign in when prompted. Use the agent in Agent mode. - VS Code: add
{"servers": {"PubNub": {"type": "http", "url": "https://mcp.pubnub.com"}}}to.vscode/mcp.jsonand sign in when prompted. - Claude Desktop, Codex, Gemini CLI, and OpenCode: see Set up the MCP server.
When you sign in, select your organization and click Allow access. Then ask your tool: "Create a PubNub app with a testing keyset for a chat tutorial. Enable Presence and Message Persistence, then show me the publish and subscribe keys." Copy both keys. You'll paste them into the code later in the build steps.
With the Admin API
To create keys from a script or a CI/CD pipeline, use the Admin API, PubNub's REST API for account management. An Account Owner or Account Admin creates a Service Integration and its API key in the Admin Portal under Organization Settings → API Management. Give it read & write access to apps and keysets, plus read access to secret keys, which keyset creation requires. With that key, create an app, then create a testing keyset in it and pass the keyset's configuration in the same request. The response includes the publish and subscribe keys. See the Admin API documentation for the requests and keyset settings.
Check Presence
However you create the keyset, open it in the Admin Portal and make sure Presence tracks All channels. The recommended setting, Selected channels only, doesn't generate presence events until you add a rule in Presence Management. Messages would still arrive, but the online count would never change. All channels is fine for a test keyset with a few users, but the portal warns that it can get expensive on a busy keyset. On a production keyset, keep Selected channels only and add a rule for chat-channel.
Build and run
Congratulations - what's next?
You have a working chat with live messages, an online count, history, display names and timestamps, all on the PubNub JavaScript SDK.
To store user profiles and channel details on PubNub, look at DataSync. It needs Access Manager, so the app gets its users' tokens from your server.
Go further with the JavaScript SDK
JavaScript SDK documentation Presence Message PersistenceBefore you go to production
This tutorial lets anyone with your keys publish and read the channel. Use Access Manager to give each user a token for the channels they may use, and give each user a stable User ID instead of a random one.
Ready to get beyond proof of concept?
Schedule a 15-minute chat with one of our experts to get your project moving.
Talk to an ExpertTake it to production on your own keys
Create a free PubNub account to get permanent keys for this app.