How do server-sent events deliver live updates?
Useful first: HTTP · optional, not a requirement
Choose your depth
Stay with the essentials, or look closer.
YOUR GOALRecognize when a one-way stream fits a page and what it does not provide.
A page that listens for updates
The browser opens the door
Imagine a delivery page showing Preparing, then Dispatched. With SSE, the browser opens an HTTP request and the server keeps sending updates in its response. The page handles each incoming event without asking for a whole new page.
This is a stream of text messages. It is not the server opening an unsolicited connection to your browser.
[3]Updates travel one way
The SSE message channel runs from server to browser. A Cancel delivery button needs a separate request to send that command back. If the design needs both sides to send messages on one channel, compare WebSockets.
These are design choices, not a claim that one technology is always faster.
[3]Stop listening when the view is finished
A delivery view can keep its EventSource object while updates are needed, then call feed.close() when the user leaves that view. This closes the connection and sets its state to CLOSED.
Closing the feed does not cancel the delivery. It ends this listener; the application needs a separate cancellation action.
[4]Use SSE for a page that listens; keep user commands and listener lifetime explicit.
An SSE connection lets the browser send commands back as SSE messages.
SSE carries server-to-browser events. Client commands need another request or a different channel.
Show the explanation
Use a separate request to the server. The stream only carries updates toward the page. Stopping the listener is not an application cancellation command.
YOUR GOALInterpret the event-stream text format and distinguish connection state from message content.
Read a message, then choose its listener
A blank line completes the message
The response uses Content-Type: text/event-stream and UTF-8 text. Here is an illustrative response-body block, including its final blank line:
event: delivery
data: parcel-7
data: dispatched
The two data lines become one string: parcel-7\ndispatched, where \n represents a newline. The event name is delivery.
The event name chooses the handler
Register a delivery listener with addEventListener for that block. A block without an event name is delivered as message, handled by onmessage.
A line starting with : is a comment. It can serve as a heartbeat without becoming a message. Data is text; JSON inside a data field needs application parsing rather than automatic conversion into an object.
Connected is a state, not a delivery receipt
readyState reports CONNECTING (0), OPEN (1), or CLOSED (2). A status indicator can use these states to distinguish waiting, listening and stopped.
OPEN describes the connection, not whether a particular delivery update was displayed or saved. During reconnection the object returns to CONNECTING; an error event does not by itself mean that all retries have ended.
Separate the text framing, event name and connection state when debugging a feed.
onmessage handles every incoming event regardless of its event field.
A named event is dispatched under that name. Register the matching listener; unnamed messages use message.
Show the explanation
It produces one delivery event with a newline between the data values. The blank line completes one block. Its event name is delivery, and the data lines are joined as text.
YOUR GOALDesign replay and deployment behavior around the actual guarantees of EventSource.
Reconnection is only part of recovery
Treat an ID as a cursor, not an acknowledgment
Consider this completed event:
id: 42
data: parcel-7 dispatched
On reconnection, a nonempty last-event ID is sent as Last-Event-ID. For this example it is 42. That mechanism supplies a position; it does not define server retention or acknowledge an application database write.
Application-design consequence: if updates 43 and 44 happened while disconnected, the service needs retained history and resume logic to send them. Decide how to handle an expired cursor and repeated updates. Exactly-once processing is not created by adding an id field.
[1]A healthy origin can still look delayed
A reverse proxy may buffer response bytes. In NGINX, proxy_buffering off passes upstream data onward as received; an X-Accel-Buffering: no response can also control buffering unless configuration ignores it.
NGINX read timeout measures the gap between upstream reads, not the total lifetime of the response. Check buffering and idle intervals when events arrive in bursts or quiet streams disconnect. A setting for one proxy is not a guarantee about every intermediary.
[6]Retries and cross-origin access have boundaries
retry: 1500 sets a reconnection delay in milliseconds; the browser may wait longer. retry: 1.5 is ignored. HTTP 204 stops reconnection. A final data block without its terminating blank line is not dispatched merely because the stream ends.
For a cross-origin EventSource using withCredentials: true, the server must permit credentialed CORS access: an appropriate explicit Access-Control-Allow-Origin and Access-Control-Allow-Credentials: true. The origin wildcard does not work in credentials mode include; this option does not bypass origin restrictions.
Test recovery across a disconnect and a proxy, not just the first successful event.
Sending id fields guarantees that reconnecting clients process every update exactly once.
IDs assist resumption. Retention, replay, duplicate handling and application completion remain separate responsibilities.
Show the explanation
Server retention and resume logic, with an application policy for gaps and duplicates. The header provides a cursor. It cannot recreate events the service did not retain or guarantee that application work occurred exactly once.
Keep asking. Check the details.
Source-checked using AI (OpenAI Codex) on . The checks covered sources, examples, and quizzes for this article version.
No human review is claimed. AI review can miss errors; check the original sources below.
- WHATWG HTML — EventSource processing, event IDs and parsing rules
- MDN — Using server-sent events and the stream format
- MDN — EventSource and one-way HTTP updates
- MDN — Closing an EventSource connection
- MDN — EventSource connection states
- NGINX — Proxy response buffering and read timeouts
- WHATWG Fetch — CORS responses and credentials