package lazysse
import "golazy.dev/lazysse"
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 Header
Header adds a response header before the stream starts.
func Header(name string, value string) Option
func Status
Status sets the HTTP status used when the stream starts.
func Status(code int) Option
type Source
Source produces events for a stream.
type Source interface {
Subscribe(context.Context, SubscribeOptions) (Subscription, error)
}
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
type SubscribeOptions
SubscribeOptions configures a subscription source.
type SubscribeOptions struct {
LastEventID string
}
type Subscription
Subscription is a live stream of events.
type Subscription interface {
Events() <-chan Event
Close() error
}
Package lazysse writes Server-Sent Events responses.
Controllers usually start streams through lazycontroller.Base.SSEStream so GoLazy can mark the response as sent, bypass automatic rendering, and avoid dynamic response buffering. Use this package directly when an ordinary http.Handler needs the same event formatting and flush behavior without a controller.