Community JavaScript Snippet

A Server-Sent Events Consumer That Reconnects

The Node-friendly SSE client I write whenever the browser `EventSource` is the wrong tool. Parses the line protocol by hand, resumes from `Last-Event-ID`, and applies bounded backoff between retries.

A Server-Sent Events Consumer That Reconnects

The Node-friendly SSE client I write whenever the browser `EventSource` is the wrong tool. Parses the line protocol by hand, resumes from `Last-Event-ID`, and applies bounded backoff between retries.

JavaScript
Frontend
2 snippets
sse
real-time
http
code-template
jordandubois

By @jordandubois

March 29, 2026

·

Updated May 18, 2026

813 views

7

4.3 (12)

The W3C SSE spec is mostly about edge cases: continuation data: lines accumulate with newlines, a leading space after the colon is consumed, lines starting with : are comments (Cloudflare uses these as keepalives), and retry: is a hint to the client about reconnect delay. Writing the parser as an async generator lets the caller iterate events without buffering the whole stream, which matters when you are tailing a 24-hour incident feed. The buffered id survives across event boundaries because the spec says id is sticky until the next id: line. I have re-implemented this enough times to know that keeping the field-parse logic in one place is worth the 30 lines.