Real-Time: Polling, Long Polling, SSE & WebSockets
Definition
Polling
- In plain English
- Polling is when the client asks the server for updates on a fixed timer.
- Think of it like
- It is like a child asking "are we there yet?" every five minutes.
Long polling
- In plain English
- Long polling is when the server holds a request open until it has news or a timeout passes.
- Think of it like
- It is like waiting at the counter after asking the shopkeeper to tell you when stock arrives.
Server-Sent Events
- In plain English
- Server-Sent Events is a one-way stream where the server keeps pushing updates over one open HTTP response.
- Think of it like
- It is like tuning into a radio broadcast.
WebSocket
- In plain English
- A WebSocket is a long-lived, two-way connection where either side can send messages at any time.
- Think of it like
- It is like an open phone call instead of swapping letters.
Why it exists
Your ticket queue changes at unpredictable times. Checking every 50 milliseconds wastes requests. Checking every minute leaves users looking at an old position.
Choose how updates travel based on the delay users can tolerate and whether messages need to flow in both directions.
How it works
| Approach | Direction | Delay | Wasted requests | Complexity | Servers between client and app |
|---|---|---|---|---|---|
| Polling | Client asks, server replies | Up to interval | Often | Low | Ordinary HTTP |
| Long polling | Client asks, server waits | Low after update | Some timeouts | Medium | Match request time limits |
| SSE | Server to client after initial request | Low | Few | Medium | Forward events without waiting to collect more |
| WebSocket | Both directions | Low | Few | Higher | Allow WebSocket setup and long idle periods |
- Ask
Client requests updates with its last known position.
- Wait
Server holds the request until data arrives or timeout expires.
- Reply
Server returns an update or an empty result.
- Repeat
Client opens the next request, using retry delay after errors.
The client requests a WebSocket upgrade, and the server replies 101 Switching Protocols. The server sends a new order; the client sends order bumped.
Examples
Stream a ticket queue position
Your concert queue page displays position 4,512 and only needs incoming updates.
SSE keeps one HTTP response open for queue updates. Number each event so reconnecting clients can ask for missed updates. The server must save those events to replay them.
HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
id: 18
event: position
data: {"position":4512}
The insight
If data flows one way, SSE is simpler than WebSockets.
Exchange kitchen orders and actions
Kitchen screens receive orders and chefs tap bump to report completion.
The service pushes new orders to kitchen screens. Chefs send frequent bump messages when orders are ready. Both sides need to speak promptly, so a WebSocket fits this conversation.
The order service and kitchen screen exchange messages in both directions over a WebSocket.
The insight
Two-way, frequent messages earn a WebSocket.
Poll a print job every 30 seconds
Print-shop customers check job status a few times before pickup.
Print customers only need occasional updates before pickup. Poll every 30 seconds if that delay meets their needs.
Stop polling when the page closes or the job finishes. This avoids keeping a connection open.
The insight
Don't build real-time when "every 30 seconds" is good enough.
Trade-offs
Pros
- Deliver updates promptly.
- Avoid frequent empty requests.
Cons
- Connections consume resources.
- Connection routing adds work.
- Clients need recovery logic.
Use when
- Updates are frequent or unpredictable.
- Seconds of delay affect decisions.
Avoid when
- Changes are rare and delay is acceptable.
- Data is needed only when opening a screen.
Quick check
Takeaways
- Polling is easy to operate but can waste requests.
- SSE = server → client stream.
- WebSocket carries messages both ways but requires managing connections that stay open.