sse

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package sse writes a server sent event stream. It is internal, because the ds package and the htmx package own the shapes that an application writes. See the SDD, S12.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("sse: the client closed the stream")

ErrClosed states that the client closed the connection. A stream returns it after a disconnect, so a handler stops its work and returns.

Functions

This section is empty.

Types

type Event

type Event struct {
	// Name is the event type. An empty name writes no event line, so the
	// client reads the frame as a message.
	Name string
	// ID is the identifier of the frame. An empty value writes no id line.
	ID string
	// Retry is the reconnection delay. A zero value writes no retry line.
	Retry time.Duration
	// Data holds one data line for each element. A line that holds a new
	// line becomes two lines, because a frame cannot carry one.
	Data []string
}

Event is one frame.

type Stream

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

Stream writes frames to one client.

func New

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

New starts a stream. It writes the headers of the stream and flushes them, so the client opens at once.

It returns an error when the writer cannot flush. A writer that buffers the whole response cannot stream, and the fault must appear before the handler writes a frame.

func (*Stream) Context

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

Context returns the context of the request.

func (*Stream) Done

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

Done returns a channel that closes when the client disconnects. A handler that waits for work selects on it, so no goroutine stays behind.

func (*Stream) Err

func (s *Stream) Err() error

Err returns the first fault of the stream, or nil.

func (*Stream) Send

func (s *Stream) Send(e Event) error

Send writes one frame and flushes it. It returns ErrClosed after the client disconnects, and it writes nothing more after a fault.

Jump to

Keyboard shortcuts

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