Mini Multiplexed Chat
One WebSocket carries chat, presence, and file chunks as labeled frames.
What you are building
This project turns the whole course into a tiny working system: one WebSocket connection carries three logical app channels.
| Channel | What it carries | Why it exists |
|---|---|---|
| chat | User chat text | Normal app messages. |
| presence | online | Small status update that should not need a second socket. |
| file | Fake file chunks | Bulk data that demonstrates chunking. |
The WebSocket is the shared road. Each JSON frame has a channel label and sequence number. The browser demultiplexes incoming frames into separate channel displays.
What the demo does
- Connect: browser opens one WebSocket to
ws://localhost:8765. - Encode: a chat, presence, or file message is split into frames.
- Send: frames travel over the same WebSocket.
- Broadcast: the teaching server sends each frame to every connected browser.
- Demux: each browser reads
channelandsequence, then rebuilds channel messages.
{ "channel": "chat", "messageId": "chat-1", "sequence": 0, "total": 2, "data": "hell" }
{ "channel": "presence", "messageId": "presence-2", "sequence": 0, "total": 2, "data": "onli" }
{ "channel": "file", "messageId": "file-3", "sequence": 0, "total": 2, "data": "ABCD" } Teaching demo, not production server
This project intentionally uses a tiny WebSocket server so the multiplexing protocol stays visible. Do not expose it to the internet. Production systems need full WebSocket frame handling, origin checks, authentication, TLS, rate limits, backpressure, ping/pong, and robust close/error handling.
Run it
node project/mini-ws-server.mjs
npx serve . Open project/mini-ws-chat.html from the served site in two browser windows. Messages in each channel share one socket.
Controls
Channels
Raw frames on one socket
Demultiplexed channels
| Channel | Progress | Missing | Message |
|---|---|---|---|
| chat | 0/0 | — | |
| presence | 0/0 | — | |
| file | 0/0 | — |
Read these in order. The protocol module is the reusable idea. The browser page shows UI and demultiplexing. The server is intentionally tiny and teaching-only.
Try these experiments
- Open two browser windows, connect both, then send chat from one window.
- Send fake file and watch it arrive as two file frames.
- Change chunk size in
sendChannel()from4to2, then predict frame order. - Add a new
typingchannel. What should its payload be?