sse

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: May 1, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleSSE

func HandleSSE(broker *Broker) forge.SSEHandler

HandleSSE returns a forge SSE handler that connects clients to the broker. The handler runs for the lifetime of the client connection, sending keep-alive comments at the configured interval. The broker broadcasts events to all connected clients. The handler exits when the client disconnects OR when the broker is closed (e.g. during application shutdown), whichever comes first.

Types

type Broker

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

Broker manages SSE client connections and broadcasts events.

func NewBroker

func NewBroker(keepAlive time.Duration, logger forge.Logger) *Broker

NewBroker creates a new SSE event broker.

func (*Broker) AddClient

func (b *Broker) AddClient(stream forge.Stream) (string, <-chan struct{})

AddClient registers a new SSE stream as a client. Returns the client ID and a channel that is closed when the client is removed (either explicitly via RemoveClient or when the broker is closed). SSE handlers should select on the done channel to exit promptly on shutdown.

func (*Broker) Broadcast

func (b *Broker) Broadcast(event Event)

Broadcast sends an event to all connected clients concurrently. Failed sends cause the client to be removed.

func (*Broker) BroadcastJSON

func (b *Broker) BroadcastJSON(eventType EventType, data any)

BroadcastJSON sends a JSON event to all connected clients.

func (*Broker) ClientCount

func (b *Broker) ClientCount() int

ClientCount returns the number of connected clients.

func (*Broker) Close

func (b *Broker) Close()

Close shuts down the broker and disconnects all clients.

func (*Broker) KeepAlive

func (b *Broker) KeepAlive() time.Duration

KeepAlive returns the configured keep-alive interval.

func (*Broker) RemoveClient

func (b *Broker) RemoveClient(id string)

RemoveClient removes a client by ID.

type Event

type Event struct {
	Type EventType `json:"type"`
	Data any       `json:"data"`
}

Event is a single SSE event to broadcast to all connected clients.

type EventType

type EventType string

EventType identifies the kind of SSE event sent to clients.

const (
	EventHealthUpdate  EventType = "health-update"
	EventMetricsUpdate EventType = "metrics-update"
	EventWidgetRefresh EventType = "widget-refresh"
	EventNotification  EventType = "notification"
	EventNavBadge      EventType = "nav-badge-update"
	EventTraceUpdate   EventType = "trace-update"
)

Jump to

Keyboard shortcuts

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