sse

package module
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 5 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 added in v0.0.15

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 ClientConfig

type ClientConfig struct {
	// Endpoint is the SSE server URL.
	Endpoint string

	// RetryInterval in milliseconds for reconnection.
	RetryInterval int

	// MaxRetryDelay caps the exponential backoff.
	MaxRetryDelay int

	// MaxReconnectAttempts limits retry attempts. 0 = unlimited.
	MaxReconnectAttempts int
}

ClientConfig holds configuration strictly for the Browser/WASM Client.

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 SSEClient

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

SSEClient is the SSE client for WASM.

func (*SSEClient) Close

func (c *SSEClient) Close()

Close closes the SSE connection.

func (*SSEClient) Connect

func (c *SSEClient) Connect()

Connect establishes a connection to the SSE endpoint.

func (*SSEClient) OnError

func (c *SSEClient) OnError(handler func(err error))

OnError sets the handler for errors.

func (*SSEClient) OnMessage

func (c *SSEClient) OnMessage(handler func(msg *SSEMessage))

OnMessage sets the handler for incoming messages.

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 added in v0.0.15

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 ServerConfig added in v0.0.15

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