sse

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Stream

func Stream(c *rex.Context, ch <-chan any, opts *StreamOptions) error

Stream sends events from the channel to the client with enhanced error handling and configuration options.

func StreamWithContext

func StreamWithContext(ctx context.Context, w http.ResponseWriter, ch <-chan any, opts *StreamOptions) error

StreamWithContext is a convenience wrapper that uses the provided context for cancellation instead of rex.Context.

Types

type Event

type Event struct {
	ID      string        // Event ID for client tracking
	Event   string        // Event type/name
	Data    any           // Event payload
	Retry   time.Duration // Reconnection time (converted to milliseconds)
	Comment string        // Optional comment (for keepalive)
}

Event represents a Server-Sent Event.

func NewEvent

func NewEvent(data any) Event

NewEvent creates a new Event with the given data.

func (Event) WithComment

func (e Event) WithComment(comment string) Event

WithComment sets a comment.

func (Event) WithEvent

func (e Event) WithEvent(event string) Event

WithEvent sets the event type.

func (Event) WithID

func (e Event) WithID(id string) Event

WithID sets the event ID.

func (Event) WithRetry

func (e Event) WithRetry(retry time.Duration) Event

WithRetry sets the retry duration.

type StreamOptions

type StreamOptions struct {
	// Headers to set on the response (merged with defaults)
	Headers map[string]string

	// Retry interval sent to client (0 to omit)
	Retry time.Duration

	// Enable automatic keepalive comments
	Keepalive bool

	// Keepalive interval (default: 15s)
	KeepaliveInterval time.Duration

	// Custom error handler
	OnError func(error)

	// Custom close handler
	OnClose func()
}

StreamOptions configures the SSE stream behavior.

func DefaultOptions

func DefaultOptions() *StreamOptions

DefaultOptions returns sensible defaults for SSE streaming.

Jump to

Keyboard shortcuts

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