Build an SSE stream

Frame an event stream

Return the correct content type and encode events as lines separated by a blank line.

9 minute lesson

~~~

Before choosing a tool or writing more code, make the requirement concrete. The status server writes one compact JSON object in a data: field rather than concatenating unframed JSON values.

An SSE response uses text/event-stream; fields such as event, id, data, and retry are line based, and a blank line dispatches one event. The useful target is not encyclopedic coverage. Make one deliberate choice, observe its consequences, and know which requirement would make you revise it.

A common failure is to write arbitrary text to a long response and assume EventSource can discover the boundaries. The happy path may still work, which makes the mistake easy to miss. set the event-stream headers and serialize every event with explicit fields and a terminating blank line. Keep the first version small enough that every important input and output remains visible.

This supports the module goal: Build an SSE feed that reconnects cleanly and can repair a gap instead of merely looking live in one browser tab. Capture the request, command, trace, screenshot, test result, or other evidence that proves the result.

Create a tiny SSE endpoint, inspect its raw response with curl, and verify that two events arrive as two browser events. Change one condition on purpose, predict the result, and compare the prediction with what actually happened.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →