middleware/sse
import "github.com/gofiber/fiber/v3/middleware/sse"
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.
Variables
ConfigDefault is the default config.
Source: middleware/sse/config.go:45
var ConfigDefault = Config{
Handler: nil,
OnClose: nil,
Retry: 0,
HeartbeatInterval: 15 * time.Second,
DisableHeartbeat: false,
}Functions
func New(config ...Config) fiber.Handler
Source: middleware/sse/sse.go:38
New creates a new SSE handler.
Types
type Config
Source: middleware/sse/config.go:13
Config defines the config for the SSE handler.
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
}Fields
Handler HandlerHandler writes events to the stream.
Required.
OnClose func(c fiber.Ctx, err error)OnClose is called after the stream handler returns or the client disconnects.
Optional. Default: nil
Retry time.DurationRetry controls the reconnection delay sent to clients. Values less than or equal to zero disable the initial retry field.
Optional. Default: 0
HeartbeatInterval time.DurationHeartbeatInterval 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
DisableHeartbeat boolDisableHeartbeat disables automatic comment heartbeats.
Optional. Default: false
type Event
Source: middleware/sse/event.go:18
Event defines a single Server-Sent Event frame.
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. Do not modify byte slices
// or raw JSON until Event returns.
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
}Fields
Data anyData is written as one or more data fields. Strings and byte slices are written as-is; other values are JSON encoded. Do not modify byte slices or raw JSON until Event returns.
ID stringID sets the SSE id field.
Name stringName sets the SSE event field.
Retry time.DurationRetry sets the SSE retry field for this event.
type Handler
Source: middleware/sse/config.go:10
Handler writes events to a single SSE stream.
type Handler func(c fiber.Ctx, stream *Stream) errortype Stream
Source: middleware/sse/sse.go:129
Stream is an active SSE response stream. Stream is safe for concurrent use.
type Stream struct {
// contains filtered or unexported fields
}func (s *Stream) Comment(comment string) error
Source: middleware/sse/sse.go:188
Comment writes one SSE comment and flushes it to the client.
func (s *Stream) Context() context.Context
Source: middleware/sse/sse.go:158
Context returns a context canceled when the stream ends or a write fails.
func (s *Stream) Done() <-chan struct{}
Source: middleware/sse/sse.go:164
Done returns a channel closed when a write fails, the server closes the response body, or the handler returns.
func (s *Stream) Err() error
Source: middleware/sse/sse.go:174
Err returns the first stream write error.
func (s *Stream) Event(event Event) error
Source: middleware/sse/sse.go:181
Event writes one SSE event and flushes it to the client.
func (s *Stream) LastEventID() string
Source: middleware/sse/sse.go:169
LastEventID returns the Last-Event-ID header value sent by the client.
func (s *Stream) Retry(retry time.Duration) error
Source: middleware/sse/sse.go:195
Retry writes an SSE retry field and flushes it to the client.