Resuming SSE events after reconnect: design Last-Event-ID replay
An SSE browser client attempts to reconnect after a dropped connection and can send the last event ID from the server in a Last-Event-ID request header. That header is a clue for finding a replay position, not an acknowledgment that the browser application safely processed the event. To reduce gaps and duplicates, design retained server logs, expired-cursor recovery, and idempotent client handling together.
An SSE browser client attempts to reconnect after a dropped connection and can send the last event ID from the server in a Last-Event-ID request header. That header is a clue for finding a replay position, not an acknowledgment that the browser application safely processed the event. To reduce gaps and duplicates, design retained server logs, expired-cursor recovery, and idempotent client handling together.
The position the browser remembers
An id: field in an SSE event block sets EventSource's last event ID string. When reopening a connection, the browser can send a nonempty ID as Last-Event-ID under the WHATWG standard. If retry: contains only ASCII digits, it sets the reconnection wait in milliseconds. A user agent may wait longer or add delay to reduce load.
Example:
id: orders-481
event: order-updated
data: {"orderId":"481","state":"packed"}The server can read Last-Event-ID: orders-481 and replay later events. The parser must receive the id: field in a complete event block terminated by a blank line. An SSE comment heartbeat without an event does not advance the position.
Define the replay contract on the server
Map event IDs to stable, ordered log positions or opaque cursors. The application defines the ID format; do not assume clients can infer order from it.
Validate Last-Event-ID on connection, check the caller's authorization for that stream, then look up events after the cursor. Treat it as untrusted input; handle malformed, cross-user, and out-of-range IDs.
Define a consistent boundary between replay and live events. The order of subscribing while new events are written during replay can cause gaps or duplicates.
Send events in order and assign unique IDs. Assume an event may be resent and make client handling idempotent by ID.
Set log retention and replay limits. Document what happens for an expired cursor, deleted event, or unknown ID: full state resynchronization, a fresh snapshot event, or an explicit error.
This is a design checklist, not a guaranteed implementation for a particular database or framework. With multiple connection nodes, ensure they share the event log and subscription state or can serve the same replay data.
An event ID is not an application acknowledgment
After the browser parses an event and updates EventSource's last ID, JavaScript event handling and asynchronous work proceed separately. Last-Event-ID can therefore represent the parser's position in the stream sent to the browser, but it does not prove a business change committed to a database or that a user saw it. This is a design boundary between browser cursor behavior and application processing.
To prevent duplicate side effects, make processing idempotent using the event ID or business-object version. If the client must acknowledge successful processing, EventSource reconnection alone may not suffice. Design a separate acknowledgment request or replayable application protocol, and decide whether client-side temporary storage is enough or server-side consumer acknowledgments are required.
Reconnect policy and operational checks
Send a suitable ASCII integer such as retry: 5000 to set the browser's reconnection delay in milliseconds. A user agent may add delay after failures, and the system should avoid a synchronized reconnect surge.
To stop EventSource reconnection, the WHATWG standard defines HTTP 204 No Content as a way to terminate the connection. Distinguish this from temporary server failure responses.
On reconnect, verify more than connection success: check that Last-Event-ID arrives, that its event exists in the log, and that replay hands off to the live subscription without a gap.
Measure expired cursors, log lag, authorization failures, slow clients, and reconnect surges separately. Do not retain events forever or let queues grow without bounds.
Automatic reconnect does not guarantee exactly-once business processing or permanent event retention. Explain recovery semantics to clients and provide a full-state fetch path if needed.
Sources and scope
[WHATWG HTML Standard: Server-sent events](https://html.spec.whatwg.org/multipage/server-sent-events.html)
[MDN: Using server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)
The standard defines browser semantics for id, retry, Last-Event-ID, reconnect, and 204 handling. Durable logs, business acknowledgments, deduplication, and expired-cursor recovery belong to the application; the recommendations here are design guidance derived from that boundary. No live server/browser reconnect integration test was performed.