Documentation
¶
Overview ¶
Package sseutil provides Server-Sent Events utilities for Go.
It includes a server-side broker for managing SSE client connections and broadcasting events, a client for consuming SSE streams, and an event builder for constructing SSE-formatted messages.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Broker ¶
type Broker struct {
// contains filtered or unexported fields
}
Broker manages Server-Sent Events client connections. It supports broadcasting events to all connected clients, sending events to specific clients, and automatic keep-alive pings.
func NewBroker ¶
func NewBroker(opts ...BrokerOption) *Broker
NewBroker creates a new SSE broker with the given options.
func (*Broker) Broadcast ¶
Broadcast sends an event to all connected clients. Events are dropped for clients whose send buffers are full.
func (*Broker) ClientCount ¶
ClientCount returns the number of currently connected clients.
type BrokerOption ¶
type BrokerOption func(*Broker)
BrokerOption configures a Broker.
func WithKeepAlive ¶
func WithKeepAlive(d time.Duration) BrokerOption
WithKeepAlive sets the interval for sending keep-alive comment lines to connected clients. The default is 30 seconds. Set to 0 to disable.
type Event ¶
type Event struct {
// ID is the event identifier. If non-empty, it is sent as the "id" field.
ID string
// Event is the event type. If non-empty, it is sent as the "event" field.
Event string
// Data is the event payload. It is always included in the SSE output.
// Multi-line data is split so each line is prefixed with "data: ".
Data string
// Retry is the reconnection interval in milliseconds.
// If zero, the retry field is omitted from the output.
Retry int
}
Event represents a Server-Sent Event with standard fields.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream is an SSE client that connects to an SSE endpoint and parses incoming events into a channel.
func Connect ¶
Connect establishes a connection to the given SSE endpoint URL and returns a Stream that emits parsed events. The context controls the lifetime of the connection.
func (*Stream) Events ¶
Events returns a receive-only channel of parsed SSE events. The channel is closed when the stream ends or Close is called.
func (*Stream) LastEventID ¶
LastEventID returns the ID of the last received event.