Build a geolocation tracking app with JS and PubNub
Share live locations on a map with the PubNub JavaScript SDK: positions as messages, last known positions from history, Presence, and optional AI facts.
Set up your environment
In this tutorial you build a location tracker that runs in the browser with the PubNub JavaScript SDK. Everyone who opens the same session link shares their location, and sees everyone else's on one map, as they move. When you join, you also see where the others were last. Optionally, a PubNub Function asks OpenAI for a few facts about where you are.
You need:
A current browser: Chrome, Firefox, Safari or Edge.
A code editor, for example Visual Studio Code
A way to serve a folder on
localhost, for example Node.js (npx serve) or Python 3 (python3 -m http.server). Browsers only share your location with pages onhttps://orlocalhost.Optional, for the location facts: an OpenAI API account and API key.
The map uses Leaflet with OpenStreetMap tiles, so you don't need a map API key.
Get the code
Clone the repository:
git clone https://github.com/PubNubDevelopers/Geolocation-Demo.git
cd Geolocation-DemoThe app is index.html, styles.css and js/app.js. The optional Function is in functions/location-facts.js.
Tested with the PubNub JavaScript SDK 13.1.2 (October 2026).
Create keys
You need a free PubNub account and a keyset with Presence and Message Persistence on. For the optional location facts you also need Functions, which every keyset can use. 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 location tracking tutorial. Enable Presence and Message Persistence, then show me the publish and subscribe keys." Copy both keys. You'll paste them into js/app.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. Positions would still arrive, but the active user count would stay at 0. 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, give the session channels a common prefix, and add a Presence rule for it.
Build and run
Congratulations - what's next?
You have a live location tracker on the PubNub JavaScript SDK: positions as messages on a session channel, the last known positions from Message Persistence, a count of active users from Presence and, optionally, a PubNub Function that adds AI-generated facts about each place.
To take it further:
Send positions as signals. A position goes out of date quickly. Signals are cheaper than messages, but limited to 64 bytes and not stored, so keep publishing a message now and then for the last known position.
Add geofences. A Function can check each position against an area and alert someone when a user enters or leaves it.
Play back a route. The positions are already in Message Persistence: fetch a user's messages for a time range and draw them as a path.
Go further with the JavaScript SDK
JavaScript SDK documentation Presence Message Persistence FunctionsBefore you go to production
Locations are personal data. This tutorial lets anyone with your keys and a session ID read the positions in that session. Use Access Manager to give each user a token for the sessions they belong to, give each user a stable User ID instead of a random one, and tell users how long you keep their positions. The location facts Function calls OpenAI with your key for every request on facts-request.*, so limit who can send them, for example with Access Manager or a rate limit in the Function.
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.