What Are Server-Sent Events? β Definition and How They Work
Learn what SSE is, how the event stream format works, how reconnection and event IDs behave, and when to prefer it over a WebSocket.
Server-sent events give a server a one-way channel to push updates to a client over an ordinary HTTP response that stays open. It is simpler than a WebSocket and sufficient whenever data only flows one direction.
1. The Format
HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
id: 1042
event: price_change
data: {"sku": "A-4192", "price": 49.90}
data: a message with no event name
: this is a comment, often used as a keepalive
Fields are line-based, and a blank line dispatches the event. That is the entire protocol.
2. Built-In Reconnection
If the connection drops, the browser client reconnects automatically. It also sends the last event identifier it received in a Last-Event-ID header, so the server can resume from that point.
Reconnection and resumption come free, where a WebSocket requires implementing both.
3. Compared With WebSockets
| Aspect | Server-sent events | WebSocket |
|---|---|---|
| Direction | Server to client only | Both |
| Protocol | Plain HTTP | Upgraded, separate framing |
| Reconnection | Automatic, with resume | Implement yourself |
| Data | Text only | Text or binary |
| Proxy compatibility | Ordinary HTTP response | Occasionally problematic |
| Complexity | Low | Higher |
4. When One-Way Is Enough
- Live dashboards and metrics.
- Notifications and alerts.
- Progress for a long-running job.
- Streaming generated text, token by token.
- Price and status updates.
In each case the client only listens. Choosing a WebSocket there buys bidirectionality that is never used and costs reconnection logic that must be written.
5. Operational Considerations
- Connections are long-lived, so servers must be able to hold many open.
- Proxies may buffer, delaying delivery; disabling buffering for the endpoint is usually required.
- Keepalives β a periodic comment line stops intermediaries from reaping an idle connection.
- Browser connection limits apply per host under HTTP/1.1; HTTP/2 multiplexing removes the constraint.
6. Where It Appears
Streaming responses from language model APIs commonly use this format, which is why the shape is now familiar well beyond dashboards. It is also used for live feeds where a client simply consumes a sequence of events.
7. Consuming It Outside a Browser
Server-side clients read the response body line by line, splitting on blank lines to assemble events. Reconnection and last-event tracking must be implemented explicitly, since only browsers provide them automatically.
8. Summary
Server-sent events stream text events from server to client over a long-lived HTTP response, with a simple line-based format and automatic reconnection with resumption in browsers.
Choose it whenever data flows one way β it is simpler than a WebSocket, works through ordinary HTTP infrastructure, and hands you reconnection for free.