Skip to content

feat: add InitSSE(), SSEStream() helpers and fix deprecated CloseNotifier in Stream() #4661

Description

@shahariaz

Feature Description

Problem

  1. c.SSEvent / c.Stream require boilerplate SSE headers on every handler.
  2. There is no combined "set up SSE + loop until disconnect" helper.
  3. c.Stream uses the deprecated http.CloseNotifier interface
    (w.CloseNotify()) which was removed from net/http in 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 / TestContextStreamWithClientGone updated

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/proposalGot an idea for a feature that Gin doesn't have currently? Submit your idea here!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions