# Server-Sent Events and Polling

> Choose the simplest real-time technique that works: short polling, long polling or one-way SSE streams. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/backend/sse-and-polling
Last updated: 2026-11-28

Choose the simplest real-time technique that works: short polling, long polling or one-way SSE streams.

Intermediate · about 3 hours · difficulty 2/5

Learn first: [HTTP in Depth](https://www.internhack.in/roadmaps/tracks/backend/http-protocol)

Not every live feature needs WebSockets. If data only flows **from server to client** (notifications, progress bars, live scores, streaming AI responses), simpler options are easier to build, cache and scale.

### Short polling

The client asks every N seconds: `GET /jobs/42/status`. Trivial to implement and works everywhere, but wastes requests when nothing changes and adds up to N seconds of delay. Perfectly fine for dashboards refreshing every 30 seconds. Use ETags so unchanged responses are cheap.

### Long polling

The client makes a request and the server **holds it open** until there is new data or a timeout (say 30 seconds), then the client immediately asks again. Near real-time with plain HTTP, but each waiting client ties up a connection, and you must handle timeouts carefully.

### Server-Sent Events

**SSE** keeps one HTTP response open and streams events in a simple text format (`event:`, `data:`, `id:` lines). The browser's `EventSource` API handles it natively and **reconnects automatically**, sending `Last-Event-ID` so the server can resume. It works through most proxies and over HTTP/2, where many streams share one connection. Limits: one-way only (client-to-server still uses normal requests), text only, and over HTTP/1.1 browsers cap connections per domain.

### Choosing

- Occasional updates, low urgency: **short polling**.
- One-way live updates, streaming responses: **SSE**.
- Frequent two-way messages, low latency: **WebSockets**.

Webhooks cover the server-to-server version of the same problem.

### Pitfalls

Proxies that buffer responses break SSE; disable buffering (for example `X-Accel-Buffering: no` in Nginx) and send periodic comment lines as keep-alives.

You are ready to move on when you can stream job progress to a browser over SSE with automatic resume after a disconnect, and justify SSE vs WebSockets for a given feature.

## Key concepts

- **Short polling**
- **Long polling**
- **Server-Sent Events format**
- **EventSource and automatic reconnect**
- **Last-Event-ID resume**
- **Proxy buffering pitfalls**
- **Choosing SSE vs WebSockets**

## Mini project

Build a report-export feature that streams progress percentages over SSE, resumes correctly after a network drop, and falls back to polling a status endpoint.

## Check yourself

Why is SSE a better fit than WebSockets for streaming an AI model's response token by token?

## Free resources

- [MDN: Using server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) (MDN)
- [HTML Standard: Server-sent events](https://html.spec.whatwg.org/multipage/server-sent-events.html) (WHATWG)

Previous: [WebSockets](https://www.internhack.in/roadmaps/tracks/backend/websockets) · Next: [Structured Logging](https://www.internhack.in/roadmaps/tracks/backend/structured-logging)

## More in Real-Time Communication

- [WebSockets](https://www.internhack.in/roadmaps/tracks/backend/websockets)
- [Server-Sent Events and Polling](https://www.internhack.in/roadmaps/tracks/backend/sse-and-polling)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
