golazy.dev golazy.dev / lazysse Index | Files

package lazysse

import "golazy.dev/lazysse"

Package lazysse writes Server-Sent Events responses.

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