Stream real-time data with PubNub in Node.js and Python
Stream Wikimedia's live changes through PubNub with the Node.js SDK, then publish hundreds of messages a second with HTTP pipelining and streaming in Python.
Set up your environment
PubNub delivers a constant stream of data, such as live edits, sensor readings or market prices, to any number of subscribers. This tutorial shows two ways to stream data through PubNub:
- With the PubNub JavaScript SDK: a Node.js app reads Wikimedia's live feed of page changes and publishes every change to a PubNub channel. The PubNub data streaming demo shows them in the browser.
- With HTTP pipelining and streaming: a Python script sends thousands of messages a second over one connection, and you watch them arrive at a streaming URL.
You need:
- Node.js 22.12 or later (the current PubNub JavaScript SDK and the eventsource package require it)
- Python 3
- Git
- A code editor, for example Visual Studio Code, and a current browser
Get the code
Clone the tutorial repository and open its folder:
git clone https://github.com/PubNubDevelopers/Real-Time-Data-Streaming-Tutorial.git
cd Real-Time-Data-Streaming-TutorialIt has three folders: real-time-pubnub-sdk (the Node.js publisher), real-time-pubnub-stream (the Python script) and demo-changes (a function you paste into the demo in the build steps).
Tested with the PubNub JavaScript SDK 13.1.3, Node.js 24 and Python 3.15 (October 2026).
Create keys
You need a free PubNub account and a keyset. The keyset's default services are all this tutorial needs. 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 data streaming tutorial, with the default services, then show me the publish and subscribe keys." Copy both keys. You'll paste them into real-time-pubnub-sdk/index.js and real-time-pubnub-stream/pubnub-stream3.py 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.
A free account covers up to 1 million transactions a month. Every publish is at least one transaction, and a payload over 2 KiB counts as one per 2 KiB part. The apps in this tutorial publish quickly (the Node.js app tens of changes a second, the Python script hundreds of messages a second), so stop them with Ctrl+C once you've seen them work.
Build and run
Congratulations - what's next?
You streamed live data through PubNub twice: with the JavaScript SDK from Node.js, and with HTTP pipelining from Python.
Go further
JavaScript SDK documentation Publish messages API limits Real-time data streaming demoBefore you go to production
Your keys are in the code, so anyone who has them can publish to your channels. Keep the publish key on your servers, and use Access Manager to give subscribers read-only tokens. For sustained high-throughput publishing, talk to PubNub about your traffic first.
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.