Realtime: WebSockets & SSE
Before this, it helps to know: DNS, HTTP, TCP in 10 Minutes, Queues & Pub/Sub.
Read a little, play a little. No scary maths, and no rush.
The scoreboard problem
You want live cricket scores on a page. Option one: ask the server "any news?" every 5 seconds (polling). With a million fans, that's 12 million pointless requests a minute, 99% answered "nope." There has to be a better way — and there are two, depending on who needs to talk.
When only the server talks: SSE
For scores, stock tickers, or build logs, data flows one way: server → browser. Server-Sent Events (SSE) hold one HTTP connection open and stream data: lines down it. Dead simple, auto-reconnects, works through most proxies. If the client never needs to send anything meaningful back, stop here — SSE is enough and every interviewer respects that restraint.
When both sides talk: WebSockets
Chat needs both directions with low latency: you type, they type, typing indicators, read receipts. A WebSocket upgrades one HTTP request into a persistent two-way socket both sides can write to anytime. The price: connections are sticky (that socket lives on one specific server), so you need heartbeats to detect dead peers and a pub/sub backplane (like Redis) so a message arriving on server A reaches a user socketed to server B.
The ladder of desperation
Polling → long-polling (server holds the request until news or timeout) → SSE → WebSockets. Each step is more capable and more machinery. In interviews, start at the bottom and climb only when the requirements force you: "one-way and simple? SSE. Truly bidirectional? WebSockets, with sticky connections and a Redis backplane."
A worked example: a chat app across many servers
Alice is connected to server 1 and Bob to server 2. Alice sends a message, which reaches server 1, but Bob's socket lives on server 2. The usual fix is a shared pub/sub channel, often Redis: server 1 publishes "new message for room 42," and every server with members of room 42 delivers it to its own connected users. The servers stay independent, and adding a server simply means one more subscriber.
Keeping connections healthy
- Heartbeats. Connections can die silently, for example when a phone loses signal. Sending a small ping every 20 to 30 seconds and expecting a reply lets both sides notice and clean up.
- Reconnect with backoff. When a connection drops, clients should wait a little longer before each retry, so a server restart is not followed by a stampede of instant reconnects.
- Resume where you left off. SSE has this built in: the browser sends a
Last-Event-IDheader on reconnect, so the server can replay what was missed. With WebSockets you design the equivalent yourself, typically a last-seen message ID.
Capacity planning
Every open connection holds memory on the server, so plan by connections, not requests. A million concurrent users means a million sockets spread across your servers, and the load balancer must support long-lived connections and the WebSocket upgrade.
The limits of SSE
SSE carries text only, flows in one direction, and under old HTTP/1.1 browsers allow only a handful of connections per domain. With HTTP/2, many streams share one connection and that limit stops mattering.
Remember this
- Match the tool to the direction: one-way = SSE, two-way = WebSockets.
- WebSockets cost stickiness, heartbeats, and cross-server fan-out — say so aloud.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
End of Messaging and Realtime
Take the section checkpoint
A short mixed quiz on all 5 chapters, to check you are ready to move on.
Related chapters
Spotted a mistake or want a topic covered? Report an issue