Server-Sent Events (SSE) 00:00:01 A newsroom keeps one response open A newsroom wants to announce stories as they break. Server-sent events let a browser receive a sequence of text events over one HTTP response. A reader's comment can take a separate request back to the newsroom. The browser's EventSource opens an HTTP request that accepts the event-stream media type. The server returns a successful response with the appropriate content type and flushes the headers so the connection can begin carrying events. A story arrives and is written into the open answer. The same answer, without a new request. A comment from the reader is an ordinary POST. Use this arrangement when the long-lived direction is server to browser. It works through HTTP infrastructure only if the server and intermediaries actually flush the stream. 00:00:52 A blank line completes an event Follow one published story from its event identifier to a browser handler. The event-stream format is line based, so boundaries matter independently of TCP packets. The number to send back after a drop. The type. Without it, onmessage is called. The payload begins. A blank line terminates the event and dispatches the completed payload to the appropriate handler. A comment line does not dispatch a news event. It can send harmless traffic on an otherwise quiet connection. A proxy can split one event across network packets or combine several in one write. The application parser uses the event format to reconstruct complete deliveries. 00:01:35 Resume after the last delivered identifier A reader loses the connection just as more stories are published. The newsroom keeps a replayable history. A stable event identifier lets the next connection ask for what came after its checkpoint. Event 12345 reaches the page. The connection breaks. The log keeps growing. The browser can send its last event identifier on the reconnection so the server can choose a replay position. The server reads 12346 to 12348 and sends them. Replay hands over to live delivery. Use stable identifiers to recognize duplicates around that transition. The server must implement replay and handle retention limits. Reconnection support alone cannot recover a story that was never stored or whose identifier is ambiguous. 00:02:26 When instant events arrive as one burst The server writes a headline immediately, but the browser seems frozen. Three headlines then arrive at once. Look at every buffer in the route before blaming the event producer. nginx collects the answer. Three events wait inside. When the buffer is full, all come at once. With buffering appropriately disabled for the event stream, flushed events can pass to the reader promptly. The example sends a comment periodically to keep an idle connection from expiring at intermediaries with suitable timeout settings. Configure buffering and timeouts for this streaming route, and test from the actual user's side of the proxy. The time of the origin write alone cannot verify delivery latency. 00:03:09 A newsroom worker goes offline Two web servers serve different readers, while the durable newsroom log assigns the shared event positions. Each server can follow that log and deliver stories to its own open responses. A story gets number 8 in the log. Both servers read it from the log. Each server writes to its own readers. Reader B loses the connection. Story 9 is appended. B reconnects to server 1 with Last-Event-ID 8 and gets 9. A returning reader can land on a different server and resume from the same history. The stable log retains the story; the long-lived HTTP response only transports it.