# Node WebSocket chat

A room chat sends messages and presence updates instantly across `general`, `music`, and `games`, then reconnects open tabs after a deployment. Tokay runs the Express and WebSocket server together as one Web Service.

Use it when your app needs long lived connections and shared process state instead of isolated HTTP requests. Recent messages deliberately stay in memory, which makes a restart easy to observe.

## Facts

- Runtime: Node.js
- Framework: Express and ws
- Service types: Web Service
- Resources: None
- Required secrets: None

## What it does

Enter your name, choose a room, and send a message from the composer. Everyone currently in that room sees it with your name and time.

**Here now** tracks connected people and the longest open connection. Join and leave notices appear in the conversation as tabs move between rooms or close.

The header shows **Connected** while the socket is healthy and **Reconnecting** while a new server version starts.

## Deploy it

Bring this folder to Tokay any way you like. Paste or upload it in the dashboard, push it with git, or hand it to your AI agent along with [our agent instructions](https://app.tokay.io/llms.txt). Tokay figures out the rest.

Working in Claude, ChatGPT, VS Code, or another MCP client? Connect the [Tokay MCP server](https://tokay.io/docs/platform-mcp) and ask your agent to deploy this example. It signs in through your browser, so there is no token to paste.

New to Tokay? The [getting started guide](https://tokay.io/getting-started) walks you through your first deploy.

## Try it

1. Open two tabs with different names and join `#music` in both.
2. Send a message from one tab, then close the other and watch **Here now** update.
3. Leave the first tab open while you deploy again. It shows **Reconnecting** and returns to `#music` when the new process is ready.

## Deploy while connected

Change some page copy or deploy the same app again. Each open tab shows **Reconnecting** and returns to the same room when the new version is ready.

Recent messages disappear because this example keeps only session memory. Add Redis or a database when messages need to survive a deployment.

## How it works

You only publish one address because the static page and `/ws` upgrade share the same HTTP server. The process keeps the latest 50 messages per room and sends a heartbeat every 30 seconds to remove dead sockets.

Incoming names and messages are bounded and rendered as text, not HTML. Add Redis or a database when the room history needs to outlive the process.

## Secrets

This public chat needs no secret. Anyone with the live URL can join one of its rooms.

## Grow from here

Try the [receipt box](/examples/node-sqlite-uploads-receipt-box) when local data should survive deployments and restarts.

## Source files

- [package.json](https://tokay.io/examples/src/node-websocket-chat/package.json)
- [public/index.html](https://tokay.io/examples/src/node-websocket-chat/public/index.html)
- [server.js](https://tokay.io/examples/src/node-websocket-chat/server.js)

## Download and source

- [Download the complete project](https://tokay.io/examples/downloads/node-websocket-chat.zip)
- Last updated: 2026-07-31
- Example revision: `52c7b7f`
- [View this revision on GitHub](https://github.com/tokayio/tokay-examples/tree/52c7b7ffd52e157375b08f1f11ff4fbe6c498642/node-websocket-chat)
- [Deploy a WebSocket server](https://tokay.io/use-cases/deploy-websocket-server)
