One Road, Many Conversations
Multiplexing means many labeled streams share one connection. Demultiplexing means sorting the mixed pieces back into their original streams.
Imagine one road into a house
A house has one driveway, but many things arrive on it: groceries, mail, pizza, and toys. The driveway does not become four driveways. Each package carries a label, so everyone knows where it belongs.
Memorable label: “Labels on one road.” Multiplexing is the one road. Demultiplexing is reading labels and putting each package in the right room.
Six-year-old version: Mix many colored toy trains onto one track. Each train car has a sticker. At the end, sort cars by sticker color.
Core idea
A logical channel is one conversation, like chat, file download, or video. A frame is a small labeled piece of that conversation. The shared connection carries frames in a mixed order:
chat #0 file #0 video #0 chat #1 file #1
The receiver does not guess. It reads each frame’s channelId and sequence, then rebuilds each conversation.
Demo: mix, then sort
Click through frames. Watch one connection carry three conversations, then watch the table rebuild each one.
Shared road
Demultiplexer state
| Channel | Pieces received | Rebuilt payload |
|---|---|---|
| chat | 0 | |
| file | 0 | |
| video | 0 |
What to notice: mixed order is safe only because each frame carries enough label data to sort it later.
Problems multiplexing solves
- Too many roads: opening one connection per conversation wastes memory, ports, handshakes, and battery.
- Idle waiting: if chat is quiet, file frames can use the same road instead of leaving it empty.
- One transport limit: browsers, proxies, devices, and networks often prefer fewer long-lived connections.
HTTP/2 uses frames and streams so multiple requests can share one TCP connection. The spec calls this “multiplexing of requests and responses” over one connection (RFC 9113). TCP also uses port/socket information to deliver incoming bytes to the right application endpoint (RFC 9293).
Exercise: run the tiny multiplexer
This lesson includes real code. From course root, run:
node --test tests/*.mjs Then open src/multiplexer.mjs. The key rule is tiny: split payloads into frames, attach channelId and sequence, interleave them, then sort by label on receive.
Knowledge check
Interview payoff
Question: “Explain multiplexing and demultiplexing in simple terms.”
Model answer: “Multiplexing combines many logical streams onto one shared connection by chopping data into labeled frames. Demultiplexing is the receiver reading those labels and rebuilding the original streams. It helps when many separate connections would waste resources or sit idle.”
Pro tip: Multiplexing is not magic parallelism. If one huge frame hogs the road, others still wait. Good protocols keep frames small and schedule fairly.
Next challenge: What happens when a huge file download shares the road with tiny chat messages? Should the file be allowed to block chat?