sse

package
v3.3.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package sse provides a small Server-Sent Events handler for Fiber.

The package focuses on the SSE transport: response headers, wire formatting, flushing, heartbeat comments, and disconnect detection via flush errors. Application-specific concerns such as topics, replay storage, authentication, and pub/sub fan-out intentionally stay outside the core middleware.

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{
	Handler:           nil,
	OnClose:           nil,
	Retry:             0,
	HeartbeatInterval: 15 * time.Second,
	DisableHeartbeat:  false,
}

ConfigDefault is the default config.

Functions

func New

func New(config ...Config) fiber.Handler

New creates a new SSE handler.

Types

type Config

type Config struct {
	// Handler writes events to the stream.
	//
	// Required.
	Handler Handler

	// OnClose is called after the stream handler returns or the client disconnects.
	//
	// Optional. Default: nil
	OnClose func(c fiber.Ctx, err error)

	// Retry controls the reconnection delay sent to clients.
	// Values less than or equal to zero disable the initial retry field.
	//
	// Optional. Default: 0
	Retry time.Duration

	// HeartbeatInterval controls comment heartbeats used to keep intermediaries
	// from closing idle streams and to detect disconnected clients.
	// When DisableHeartbeat is false, values less than or equal to zero are
	// replaced by the default interval.
	//
	// Optional. Default: 15 * time.Second
	HeartbeatInterval time.Duration

	// DisableHeartbeat disables automatic comment heartbeats.
	//
	// Optional. Default: false
	DisableHeartbeat bool
}

Config defines the config for the SSE handler.

type Event

type Event struct {
	// Data is written as one or more data fields. Strings and byte slices are
	// written as-is; other values are JSON encoded.
	Data any

	// ID sets the SSE id field.
	ID string

	// Name sets the SSE event field.
	Name string

	// Retry sets the SSE retry field for this event.
	Retry time.Duration
}

Event defines a single Server-Sent Event frame.

type Handler

type Handler func(c fiber.Ctx, stream *Stream) error

Handler writes events to a single SSE stream.

type Stream

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

Stream is an active SSE response stream.

func (*Stream) Comment

func (s *Stream) Comment(comment string) error

Comment writes one SSE comment and flushes it to the client.

func (*Stream) Context

func (s *Stream) Context() context.Context

Context returns a context canceled when the stream ends or a write fails.

func (*Stream) Done

func (s *Stream) Done() <-chan struct{}

Done returns a channel closed when a write fails or the handler returns.

func (*Stream) Err

func (s *Stream) Err() error

Err returns the first stream write error.

func (*Stream) Event

func (s *Stream) Event(event Event) error

Event writes one SSE event and flushes it to the client.

func (*Stream) LastEventID

func (s *Stream) LastEventID() string

LastEventID returns the Last-Event-ID header value sent by the client.

func (*Stream) Retry

func (s *Stream) Retry(retry time.Duration) error

Retry writes an SSE retry field and flushes it to the client.

Jump to

Keyboard shortcuts

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