Polling, long polling, SSE and WebSockets
Four ways to get live updates to a browser, and how to choose between them.
On the roadmap:Beginner · step 8: Real-time basics
Lesson 8 of 23 · 7 min read · quiz · exercise
The problem
HTTP is request and response: the client asks. How does the server tell a browser something now?
The four options
| Technique | How | Good | Weak |
|---|---|---|---|
| Short polling | Client asks every N seconds | Trivial, works everywhere | Wasteful, delayed by N |
| Long polling | Client asks; the server holds the request until there is news (or a timeout), then the client asks again | Near real time, plain HTTP | A held request per client, awkward at scale |
| Server-sent events (SSE) | One long HTTP response streams events | Simple, auto-reconnect, text over HTTP | One-way only |
| WebSockets | HTTP upgrades to a two-way connection | Low latency both ways | Stateful connections, more infrastructure |
Figure SDA-CORE-08-1 · The four options · © forcsguy.com
Before: polling: keep asking
After: WebSocket: server pushes
What changed
- No wasted requests when nothing has changed
- Updates arrive the moment they happen
- The server must hold many open connections
Choosing
- Updates are rare or delays are fine: polling.
- The server pushes, the client mostly listens (feeds, scores, notifications, progress): SSE.
- Both sides send often (chat, games, collaborative editing): WebSockets.
Running persistent connections
Connections are long-lived, so capacity is about how many connections a server can hold, not requests per second.
- Put connection handling in servers that are separate from your stateless API.
- Use heartbeats to detect dead connections, and reconnect with backoff and jitter.
- Track which server holds which user so messages can be routed (a presence or session store), and use a pub/sub system between servers.
- When a client reconnects, let it ask for what it missed (events after id X), because connections drop.
- Authenticate on connect and again when tokens expire.
Related reading
- RFC 6455: The WebSocket ProtocolIETFDocumentation
- MDN: WebSockets APIMDN Contributors · MozillaDocumentation
- Uber's Real-Time Push PlatformUber Engineering · UberCase Studies
- How Discord Scaled Elixir to 5,000,000 Concurrent UsersDiscordCase Studies
Show 6 moreShow fewer
- The WhatsApp Architecture Facebook Bought for $19BHigh ScalabilityCase Studies
- Erlang at Facebook (slides)Eugene Letuchy · FacebookCase Studies
- Slack EngineeringSlack engineers · SlackBlogs
- Real-time MessagingSlack Engineering · SlackCase Studies
- How Uber Scales Their Real-time Market Platform - High Scalability -High Scalability engineers · High ScalabilityCase Studies
- Real-Time Predictions at DoorDashDoorDash engineers · DoorDashCase Studies
Check your understanding
Question 1 of 3
0 correct so far
What is the drawback of short polling every second?
Topics covered in this lesson
Practise it: Hold the connections
Everything below is already set up for this lesson: change the design, run the traffic, and see whether you meet the goal.
- 1The task
- 2Build and test
Real-time messages arrive at 2,500 per second through one WebSocket gateway, which can only carry about 1,500 a second.
Goal: Under 1% errors, queue clears in 30 s.
Select the WebSocket gateway and look at its instances.
Try it yourself
Choose a technique for each: (a) a live sports score ticker, (b) a chat app, (c) a nightly report status that changes every few minutes.
Click what a strong design here would need:
More in Building blocks
© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.