sse

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 7 Imported by: 2

README

tinysse

Client and Event Server, SSE (Server-Sent Events) protocol compatible with Go (Server) and TinyGo (WASM Client).

Get Started

Installation

go get github.com/tinywasm/sse

Quick Links

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SSEMessageModel = model.Definition{
	Name: "ssemessage",
	Fields: []model.Field{
		{Name: "id", Type: model.Text()},
		{Name: "event", Type: model.Text()},
		{Name: "data", Type: model.Blob()},
	},
}

Functions

func New

func New(c *Config) *tinySSE

New creates a new tinySSE instance with shared configuration.

Types

type ChannelProvider

type ChannelProvider interface {
	// ResolveChannels extracts channels for an SSE connection.
	// Called once when client connects.
	//
	// Parameters:
	//   - ctx: The router context (contains headers, path, cookies)
	//
	// Returns:
	//   - channels: List of channels to subscribe (e.g., ["all", "user:123", "role:admin"])
	//   - err: If non-nil, connection is rejected with 401/403
	ResolveChannels(ctx router.Context) (channels []string, err error)
}

ChannelProvider resolves SSE channels for a connection. Implemented by external packages (e.g., crudp session handler).

type Config

type Config struct {
	// Log is the centralized logger function.
	// If nil, logging is disabled.
	Log func(args ...any)
}

Config holds the shared configuration for both Server and Client.

type Publisher added in v0.1.0

type Publisher struct {
	Server *SSEServer
}

Publisher adapts an *SSEServer to events.Publisher. It is a separate wrapper type — NOT a method named Publish directly on *SSEServer — because SSEServer already has Publish(data []byte, channel string): a byte-level API existing callers use today. Colliding the name would either break them or force events.Publisher's single-arg shape onto that call site. Composition, not renaming, is the fix.

func (Publisher) Publish added in v0.1.0

func (p Publisher) Publish(e events.Event)

Publish encodes e.Payload with the ecosystem's typed codec (never `any`, never a hand-rolled format) and pushes it as an SSE message: Topic becomes BOTH the SSE "event" name (so a browser listener can dispatch by type) and the channel (so only connections ChannelProvider resolved into that channel receive it). A nil or IsNil Payload sends an empty body — Topic alone is still meaningful (e.g. a "heartbeat" event with no data).

type SSEMessage

type SSEMessage struct {
	Id    string
	Event string
	Data  []byte
}

func (*SSEMessage) DecodeFields added in v0.0.14

func (m *SSEMessage) DecodeFields(r model.FieldReader)

func (*SSEMessage) EncodeFields added in v0.0.14

func (m *SSEMessage) EncodeFields(w model.FieldWriter)

func (*SSEMessage) IsNil added in v0.0.14

func (m *SSEMessage) IsNil() bool

func (*SSEMessage) ModelName added in v0.0.14

func (m *SSEMessage) ModelName() string

func (*SSEMessage) Pointers added in v0.0.14

func (m *SSEMessage) Pointers() []any

func (*SSEMessage) Schema added in v0.0.14

func (m *SSEMessage) Schema() []model.Field

func (*SSEMessage) Validate added in v0.0.15

func (m *SSEMessage) Validate(action byte) error

type SSEMessageList added in v0.0.14

type SSEMessageList []*SSEMessage

func (*SSEMessageList) Append added in v0.0.14

func (s *SSEMessageList) Append() model.Fielder

func (*SSEMessageList) At added in v0.0.14

func (s *SSEMessageList) At(i int) model.Fielder

func (*SSEMessageList) DecodeFields added in v0.0.14

func (s *SSEMessageList) DecodeFields(_ model.FieldReader)

func (*SSEMessageList) EncodeFields added in v0.0.14

func (s *SSEMessageList) EncodeFields(_ model.FieldWriter)

func (*SSEMessageList) IsNil added in v0.0.14

func (s *SSEMessageList) IsNil() bool

func (*SSEMessageList) Len added in v0.0.14

func (s *SSEMessageList) Len() int

func (*SSEMessageList) Pointers added in v0.0.14

func (s *SSEMessageList) Pointers() []any

func (*SSEMessageList) Schema added in v0.0.14

func (s *SSEMessageList) Schema() []model.Field

type SSEPublisher

type SSEPublisher interface {
	// Publish sends data to clients subscribed to the specified channels.
	// Data can contain newlines - tinysse handles them internally.
	Publish(data []byte, channels ...string)

	// PublishEvent sends data with an event type for client-side routing.
	PublishEvent(event string, data []byte, channels ...string)
}

SSEPublisher allows publishing messages to SSE clients. Implemented by sse.SSEServer.

type SSEServer

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

SSEServer handles Server-Sent Events streaming connections.

func (*SSEServer) Publish

func (s *SSEServer) Publish(data []byte, channel string)

Publish sends data to a single channel.

func (*SSEServer) PublishEvent

func (s *SSEServer) PublishEvent(event string, data []byte, channels ...string)

PublishEvent implements SSEPublisher.PublishEvent.

func (*SSEServer) StreamHandler added in v0.0.15

func (s *SSEServer) StreamHandler() router.StreamFunc

StreamHandler returns a router.StreamFunc that serves SSE to a connected Streamer. Register it with: r.Stream(path, server.StreamHandler())

type ServerConfig

type ServerConfig struct {
	// ClientChannelBuffer prevents blocking on slow clients.
	// Recommended: 10-100.
	ClientChannelBuffer int

	// HistoryReplayBuffer manages the "Last-Event-ID" replay history.
	// Recommended: Depends on message frequency.
	HistoryReplayBuffer int

	// ReplayAllOnConnect replays the full history buffer to every new client
	// on first connect (when no Last-Event-ID is provided).
	// Useful for log viewers where clients may connect after events are published.
	ReplayAllOnConnect bool

	// ChannelProvider resolves channels for each SSE connection.
	// If nil, a default provider is used that rejects all connections
	// with error "channel provider not configured".
	ChannelProvider ChannelProvider
}

ServerConfig holds configuration strictly for the SSE stream handler.

Jump to

Keyboard shortcuts

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