Build a collaborative drawing app with JavaScript

Build CoDoodler, a shared canvas with the PubNub JavaScript SDK: real-time strokes, live cursors with signals, names in Presence state and who's online.

Step 01

Set up your environment

In this tutorial you look at how CoDoodler is built: a drawing app in the browser where everyone on the page draws on the same canvas. You see the others' strokes, their cursors and their names as they move, all through the PubNub JavaScript SDK. It's plain HTML, CSS and JavaScript with the Canvas API, with no build step and no server. You need:

Get the code

Clone the repository and open its folder:

Code
git clone https://github.com/PubNubDevelopers/collaboration-demo.git
cd collaboration-demo

The app is in four files: index.html (the page), js/config.js (your keys and the PubNub client), js/app.js (the app logic) and timeout.html (shown when you've been idle). The build steps explain the key parts of js/app.js; the file has the complete code.

Tested with the PubNub JavaScript SDK 13.1.2 (October 2026).

Step 02

Create keys

You need a free PubNub account and a keyset with Presence on. 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, start claude, 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.json and 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 collaborative drawing tutorial. Enable Presence, then show me the publish and subscribe keys." Copy both keys. You'll paste them into js/config.js 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. Drawings would still sync, but the other users' names, the user count, and the cleanup when someone leaves wouldn't work. 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 collaboration-demo.

Step 03

Build and run

Step 04

Congratulations - what's next?

You have a shared canvas where everyone sees the others' strokes, cursors and names as they happen, and who is on the canvas: messages for the drawing, signals for the cursors and Presence for the users, all on the PubNub JavaScript SDK.

A user who joins late sees an empty canvas, because the strokes are only sent to whoever is subscribed. If Message Persistence is on in your keyset (the Admin Portal turns it on for new keysets, with 7 days on a Testing keyset), then to show the drawing so far you can load the stroke messages with fetchMessages when the page opens. It returns up to 100 messages per call, so page through them, and start drawing after the last Clear All.

Go further with the JavaScript SDK

JavaScript SDK documentation Signals Presence More tutorials

Before you go to production

This tutorial lets anyone with your keys publish to the channel and set any Presence state. 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 Expert
Keep building

Take it to production on your own keys

Create a free PubNub account to get permanent keys for this app.