The server keeps one door open and pushes updates to you.
Server-Sent Events (SSE) let a server push updates to a browser over one long-lived HTTP connection. The browser opens the door once. The server keeps sending news through it.
text/event-stream.Normally, an HTTP response ends. With SSE, the server never finishes the response. It keeps writing small pieces of text. The browser reads each piece as an event, as soon as it arrives.
id: 42 event: score data: SIX! India 187/4 id: 43 event: score data: Single. India 188/4 : this is a comment, often used as a heartbeat
data: is the message. It can span several lines.id: lets the browser remember where it stopped.event: is an optional name, so different events can have different handlers.retry: tells the browser how many milliseconds to wait before reconnecting.Many features only need the server to talk: a live score, a stock price, a progress bar, a new notification. For these:
const es = new EventSource("/events");
es.onmessage = (e) => console.log("update:", e.data);
es.addEventListener("score", (e) => showScore(JSON.parse(e.data)));
es.onerror = () => console.log("lost it, browser will retry");SSE carries text only (send JSON as text). Over HTTP/1.1, browsers allow only about 6 connections per site, so many tabs can use them up. HTTP/2 shares many streams on one connection and removes this problem.
The plain EventSource cannot set custom headers, such as an Authorization header. People use cookies, or a fetch-based stream library.
: waiting for the first event
(none yet: drop the connection to see one)
Content-Type: text/event-stream, Cache-Control: no-cache, and Connection: keep-alive on HTTP/1.1.: ping) every 15 to 30 seconds. It stops proxies from closing an idle connection.proxy_buffering off), or events will arrive in clumps.| Term | Meaning in easy words |
|---|---|
| EventSource | The browser object that opens an SSE connection and handles reconnects |
| text/event-stream | The content type that tells the browser this is an SSE stream |
| Event | One message: lines like id, event, data, ended by a blank line |
| Last-Event-ID | A request header the browser sends on reconnect, to resume after the last id it saw |
| retry | A field that sets how many milliseconds the browser waits before reconnecting |
| Heartbeat | A tiny comment line sent often so the connection does not look idle |
| Push | The server sends data without being asked each time |
| Fan-out | Sending one event to many connected clients |
| Proxy buffering | A proxy holding data until it has a full chunk. It breaks live streams |
| Long polling | SSE | WebSockets | |
|---|---|---|---|
| Direction | Server to client | Server to client | Both ways |
| Data type | Anything over HTTP | Text | Text and binary |
| Reconnect | You build it | Built in | You build it |
| Resume after drop | You build it | Built in (Last-Event-ID) | You build it |
| Overhead per message | High | Low | Lowest |
| Works through proxies | Best | Good (turn off buffering) | Sometimes blocked |
| Choose it for | Fallback | Feeds, alerts, progress | Chat, games, editors |
text/event-stream with id, event and data lines, and a blank line between events.