Last-Event-ID
The HTTP Last-Event-ID request header carries the ID of the last event received by the client when reconnecting to a Server-Sent Events stream, allowing the server to resume transmission from the correct position.
Usage
Server-Sent Events (SSE) provides a standard mechanism for servers to push real-time updates to clients over HTTP. Connections sometimes drop due to network instability, timeouts, or server restarts. The Last-Event-ID header enables automatic resumption of event streams after connection loss.
When an SSE stream includes an id field in its events, the browser's
EventSource API stores this value. If the connection drops and the
EventSource attempts to reconnect, the browser includes the
Last-Event-ID header in the reconnection request. The server reads
this header and resumes sending events from the appropriate position in
the stream, preventing data loss or duplicate delivery.
The header value is a UTF-8 encoded string matching the id field from
the last successfully received event. Servers typically use this ID to
query their event log or buffer and determine which events the client
still needs. An empty id field in the event stream resets the stored
ID, resulting in no Last-Event-ID header being sent during the next
reconnection attempt.
Values
The Last-Event-ID header contains a single string value matching the event ID sent by the server in the SSE stream. The value is any UTF-8 encoded text excluding null characters (U+0000), line feeds (U+000A), and carriage returns (U+000D).
Event IDs are typically sequential integers, timestamps, UUIDs, or database cursor tokens, depending on how the server generates and tracks events.
Example
A client receiving events from a notification stream drops the
connection after receiving an event with id: 1234. When the
EventSource reconnects, the browser sends the stored ID back to the
server.
Last-Event-ID: 1234
The server uses this ID to locate the next event in its buffer and resumes sending events from the stored position forward, maintaining stream continuity without client intervention.
Takeaway
The Last-Event-ID request header reports the last received event ID to the server during reconnection, enabling automatic and reliable resumption of Server-Sent Events streams as defined in the HTML standard.