feat: add InitSSE(), SSEStream() helpers and fix deprecated CloseNotifier in Stream()
Author: shahariazCreated May 22, 2026Updated Jul 25, 2026
Labelstype/proposal
Feature Description
Problem
c.SSEvent/c.Streamrequire boilerplate SSE headers on every handler.- There is no combined "set up SSE + loop until disconnect" helper.
c.Streamuses the deprecatedhttp.CloseNotifierinterface (w.CloseNotify()) which was removed fromnet/httpin Go 1.20+.
Proposed Changes
c.InitSSE() (new)
Sets Content-Type: text/event-stream, Cache-Control: no-cache,
Connection: keep-alive and flushes headers immediately — eliminating
the copy-paste boilerplate from every SSE handler.
c.SSEStream(step func(*Context) bool) bool (new)
Calls InitSSE(), then loops calling step and flushing after each call.
Returns true when the client disconnects (via Request.Context().Done()),
false when step returns false. Symmetric with the existing Stream().
c.Stream() fix
Replaces w.CloseNotify() with c.Request.Context().Done() — the modern,
non-deprecated way to detect client disconnection.
Alternatives Considered
Users can call c.Render directly, but the ergonomics are poor for the
common case. No library-level SSE loop helper currently exists.
Checklist
- Tests added for all three changes
- Existing
TestContextStream/TestContextStreamWithClientGoneupdated
Source: gin-gonic/gin