Real-Time Event Counter & Multi-Room Chat Application built with WebSockets Node.js | WebSocket API | Full-Duplex | Docker
A hands-on reference project designed to learn core WebSocket mechanics: persistent connections, full-duplex bi-directional streaming, room-based broadcasting, and connection state management.
ChatRoom demonstrates how WebSockets eliminate HTTP polling overhead by establishing a single long-lived TCP connection. Clients can join isolated room channels, exchange live chat messages, and trigger real-time counter updates synchronized across all peers in the room instantly.
The layout of the project files and their key responsibilities:
ChatRoom/
├── package.json # Project metadata, dependencies (express, ws), and scripts
├── Dockerfile # Container configuration for the Node.js application
├── server.js # HTTP server, WebSocket handshake, room manager & broadcaster
└── public/
├── index.html # UI layout for chat streams, rooms list, and counter button
└── app.js # Client-side WebSocket client (new WebSocket, handlers, DOM update)
- HTTP Upgrade Handshake: Understand how TCP socket connections are initialized over HTTP ports.
- Room Channel Isolation: Learn selective event targeting vs global server broadcasting.
- Bidirectional Streaming: Handle client actions (publish) and server state sync (push) asynchronously.
- Presence & Heartbeats: Manage lifecycle states (connect, reconnect, close) cleanly.
Join Room:
{
"type": "JOIN_ROOM",
"payload": { "room": "general", "username": "Hamid" }
}Send Chat Message:
{
"type": "SEND_MESSAGE",
"payload": { "text": "Hello WebSockets!" }
}Broadcast Message / Counter Update:
{
"type": "COUNTER_UPDATED",
"payload": { "count": 42, "updatedBy": "Hamid" }
}No local Node.js installation is required.
- Build the Docker Image:
docker build -t chatroom-app . - Run the Container:
docker run -p 3000:3000 chatroom-app
- Access the Application:
Open
http://localhost:3000in multiple browser tabs to test real-time multi-client syncing.