Networking & API DesignIntermediate15 min read2 questions

Real-Time Communication: Polling, SSE and WebSockets

Four ways to push data to a client, the scaling cost of each, and how to handle the hard part - a million long-lived connections that all reconnect at once.

Covers: Short and long polling, Server-Sent Events, WebSockets, WebRTC, connection scaling, presence, reconnection and backfill

HTTP is request/response: the client asks, the server answers. Chat, notifications, live scores and collaborative editing all need the opposite - the server has news and must tell the client. There are exactly four mainstream ways to do that, and choosing between them is a standard interview beat.

Filter
0/2 mastered
IntermediatewebsocketsssepollingAsked at Slack, Discord

30-second answer

Short polling asks on a timer - simple, stateless, wasteful, and latency equal to half the interval. Long polling holds the request open until there is news, giving near-real-time delivery over ordinary HTTP but tying up a connection per client. Server-Sent Events is a one-way server-to-client stream over plain HTTP with automatic reconnection and event IDs built in, which covers most notification use cases. WebSockets give a full-duplex persistent connection and are the right answer only when the client also needs to send frequently - chat, collaborative editing, multiplayer. Choose the simplest one that meets the latency requirement, because each step up costs you statefulness.

AdvancedwebsocketsscalingstatefulAsked at Slack, Discord

30-second answer

Separate the connection tier from the business logic. Stateless gateway nodes hold the sockets and do nothing but authenticate, decode and forward; a registry maps user ID to the gateway currently holding that user's connection; and a pub/sub layer fans messages out to the right gateway. Each node can hold roughly 50,000–200,000 connections given enough file descriptors and memory, so a million connections is 10–20 nodes. The genuinely hard parts are not throughput but reconnect storms, deploys that drop every connection at once, and presence - knowing who is actually online.

Showing 2 of 2 questions for realtime-communication-websockets-sse-polling.

Check your understanding

4 questions · no sign-up, nothing stored

0/4 answered
Question 1

1.You need to stream progress updates for a long-running export job to a browser. The client sends nothing. Best choice?

Question 2

2.A gateway holding 100,000 WebSocket connections crashes. Which client behaviour prevents a cascading failure?

Question 3

3.Why keep business logic out of the WebSocket gateway tier?

Question 4

4.What makes reconnects gap-free after a network blip?

Hands-on challenge

Build it - this is what you talk about in a deep-dive round.

Design the real-time layer for a collaborative document editor

Multiple users edit one document simultaneously and see each other's cursors. Design only the transport and connection layer - not the conflict-resolution algorithm.

Requirements

  • Choose a transport for edits and for cursor positions, and justify treating them differently.
  • Design the gateway, registry and pub/sub topology, and state how a message reaches exactly the right sockets.
  • Specify the reconnect protocol so no edit is lost after a 10-second disconnection.
  • Define presence semantics and be explicit about what is approximate.
  • Describe a zero-disruption deploy for the connection tier.

Stretch goals

  • Handle a document with 10,000 concurrent viewers and 5 editors.
  • Add cursor-position throttling and explain the bandwidth saving.