golazy.dev golazy.dev / lazysse Index | Files

package lazysse

import "golazy.dev/lazysse"

Package lazysse writes Server-Sent Events responses.

Server-Sent Events, often called SSE, are long-lived HTTP responses that let a server push ordered text frames to a browser EventSource or another client that understands the text/event-stream format. A lazysse Stream sets the required response headers, commits the response, writes each event frame, and flushes after every send. Event data can be written directly with Send, as JSON with JSON, as comments with Comment, or by forwarding a Subscription from Subscribe.

Streams require an http.ResponseWriter that can flush. Start also recognizes buffered GoLazy response writers that expose StartStream or Unwrap, so a stream can bypass response buffering after headers are committed. That is the path used by lazycontroller.Base.SSEStream: controllers call that helper, the controller marks the response as sent, and lazydispatch does not try to render a template, buffer the body, or calculate a dynamic ETag for the stream.

Use this package directly outside a GoLazy app when a plain net/http handler needs the same SSE formatting and flush behavior. The package does not own an event queue; callers either send events themselves or provide a Source whose Subscription yields lazysse.Event values. LastEventID reads the browser's Last-Event-ID header so a Source can resume after a reconnect.

Functions

func Serve

Serve starts a stream, runs fn, and closes the stream when fn returns.

func Serve(w http.ResponseWriter, r *http.Request, fn func(*Stream) error) error

Types

type Event

Event is one Server-Sent Event frame.

type Event struct {
	Event	string
	ID	string
	Data	[]string
	Comment	[]string
	Retry	time.Duration
}

type Option

Option configures a stream before it starts.

type Option func(*options)
func Status

Status sets the HTTP status used when the stream starts.

func Status(code int) Option

type Stream

Stream writes SSE frames to a response.

type Stream struct {
	// contains filtered or unexported fields
}
func Start

Start starts an SSE response.

func Start(w http.ResponseWriter, r *http.Request, opts ...Option) (*Stream, error)
func (s *Stream) Close

Close stops stream helpers such as heartbeats.

func (s *Stream) Close() error
func (s *Stream) Comment

Comment sends an SSE comment.

func (s *Stream) Comment(text string) error
func (s *Stream) Context

Context returns the stream context.

func (s *Stream) Context() context.Context
func (s *Stream) Done

Done is closed when the client disconnects or the stream is closed.

func (s *Stream) Done() <-chan struct{}
func (s *Stream) Heartbeat

Heartbeat writes SSE comments on interval until the stream is closed.

func (s *Stream) Heartbeat(interval time.Duration)
func (s *Stream) JSON

JSON marshals value and sends it as event data.

func (s *Stream) JSON(name string, value any) error
func (s *Stream) LastEventID

LastEventID returns the browser's Last-Event-ID request header.

func (s *Stream) LastEventID() (string, bool)
func (s *Stream) Send

Send writes and flushes one event.

func (s *Stream) Send(event Event) error
func (s *Stream) Subscribe

Subscribe forwards events from source until the subscription or stream ends.

func (s *Stream) Subscribe(source Source, opts SubscribeOptions) error