sse

package
v0.0.1-rc5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package sse provides a small, dependency-free helper for writing Server-Sent Events that follows the WHATWG `text/event-stream` spec.

SPECIFICATIONS.md §5.6: events carry an `id` for `Last-Event-ID` reconnect, a `retry` hint, and we emit a periodic comment heartbeat (`:keepalive\n\n`) to keep idle proxies open.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LastEventID

func LastEventID(r *http.Request) string

LastEventID returns the value of the Last-Event-ID header, or empty.

Types

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer represents a single SSE connection. Use New() to construct one.

func New

func New(w http.ResponseWriter) (*Writer, error)

New writes the standard SSE response headers and returns a Writer. It returns an error if the underlying ResponseWriter does not support flushing (e.g. when used behind an http.ResponseRecorder).

func (*Writer) Heartbeat

func (s *Writer) Heartbeat() error

Heartbeat writes a comment line so intermediaries don't time out.

func (*Writer) HeartbeatLoop

func (s *Writer) HeartbeatLoop(ctx context.Context, interval time.Duration)

HeartbeatLoop emits a heartbeat every interval until ctx is cancelled or the writer fails. Run it from a goroutine alongside your event pump.

func (*Writer) Send

func (s *Writer) Send(eventName string, data []byte) error

Send emits a structured event. eventName may be empty (default "message"). data is written verbatim — the caller marshals JSON.

func (*Writer) SetRetry

func (s *Writer) SetRetry(d time.Duration) error

SetRetry instructs the browser EventSource to wait at least d before reconnecting after a drop. Sent as `retry: <ms>`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL