sse

package
v0.0.0-...-8acab51 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package sse provides a Server-Sent Events broker for pushing real-time events to connected clients, scoped by user ID.

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 per-user SSE client channels.

func NewBroker

func NewBroker() *Broker

NewBroker creates a new SSE broker.

func (*Broker) Broadcast

func (b *Broker) Broadcast(eventType string, data any)

Broadcast sends an event to all connected clients across all users.

func (*Broker) ClientCount

func (b *Broker) ClientCount(userID string) int

ClientCount returns the number of connected clients for a user.

func (*Broker) Close

func (b *Broker) Close()

Close signals all SSE handlers to exit and closes all client channels.

func (*Broker) Done

func (b *Broker) Done() <-chan struct{}

Done returns a channel that is closed when the broker is shut down.

func (*Broker) Publish

func (b *Broker) Publish(userID string, eventType string, data any)

Publish sends an event to all subscribers of the given user. Non-blocking: if a client's buffer is full, the event is dropped.

func (*Broker) Subscribe

func (b *Broker) Subscribe(userID string) chan Event

Subscribe creates a new event channel for the given user. The caller must call Unsubscribe when done.

func (*Broker) Unsubscribe

func (b *Broker) Unsubscribe(userID string, ch chan Event)

Unsubscribe removes a client channel. Only closes the channel if it hasn't already been closed by Close().

type Event

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

Event represents a server-sent event.

func (Event) MarshalData

func (e Event) MarshalData() []byte

MarshalData returns the JSON-encoded data field.

type Handler

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

Handler serves the SSE event stream endpoint.

func NewHandler

func NewHandler(broker *Broker) *Handler

NewHandler creates a new SSE handler.

func (*Handler) RegisterRoutes

func (h *Handler) RegisterRoutes(g *echo.Group)

RegisterRoutes registers the SSE endpoint on the given echo group.

func (*Handler) Stream

func (h *Handler) Stream(c echo.Context) error

Stream handles GET /api/v1/events — authenticated SSE endpoint.

Jump to

Keyboard shortcuts

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