Build an SSE stream

Heartbeat and clean up

Keep intermediaries aware of quiet streams and release resources as soon as a client leaves.

9 minute lesson

~~~

A comment heartbeat keeps the connection active while an abort signal removes the subscriber from the server set. This is a small part of a live service-status board that streams incidents without losing its ordinary HTTP foundation, but the boundary it creates affects everything that follows.

Long-lived responses cross browsers, proxies, servers, and load balancers, each with timeouts and resource limits. The useful target is not encyclopedic coverage. Make one deliberate choice, observe its consequences, and know which requirement would make you revise it.

Watch for one shortcut in particular: trying to create an interval per client and never clear it after navigation or network loss. It makes later failures harder to locate. Instead, send a modest heartbeat, observe the request abort signal, and remove timers and subscriptions during cleanup. Prefer an implementation you can explain from the outside before optimizing it.

Keep the wider goal in view: Build an SSE feed that reconnects cleanly and can repair a gap instead of merely looking live in one browser tab. A short observation with concrete evidence is more useful than a confident sentence with no reproduction path.

Open and close twenty clients while tracking active connections, timers, and memory; the counts must return to baseline. Hand the result to someone else and see whether the behavior is clear without an oral explanation.

Lesson completed

Take this course offline

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

Get the download library →