Server-Sent Events (SSE) with heartbeats and client cleanup

974
0

SSE is my go-to for “live updates” when I don’t need full bidirectional WebSockets. The key is to set the right headers (Content-Type: text/event-stream, Cache-Control: no-cache) and to flush periodically so intermediaries don’t buffer. I send heartbeats as : comments every ~15 seconds to keep load balancers happy and to detect dead clients. I also remove clients when r.Context().Done() fires, which prevents leaking channels and goroutines over time. The nice part is that SSE stays friendly to existing HTTP infrastructure and works well through proxies. In production I also cap the per-client buffer and drop slow consumers rather than letting them back up the whole broadcaster.