Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config[T any] struct { // How often to flush the events to the connected clients. Default is 256ms FlushFrequency time.Duration // How long to wait for all connected client to gracefully close. Default is 30s CloseTimeout time.Duration // Replay events when a client connects Replay struct { // How many events to send in chunks, when a client connects. Default is 256 events Initial int // How many events to keep in memory. Default is 2048 events Maximum int // How long an event should be kept in memory for. Default is 30s Expiry time.Duration } // Events encoder function, which returns a slice of bytes that will then be converted to a string. Default is json.Marshal() Encoder func([]T) ([]byte, error) }
Config defines the configuration settings for the Server-Sent Events (SSE) handler.
type Handler ¶
type Handler[T any] struct { // contains filtered or unexported fields }
Handler is a generic Server-Sent Events (SSE) handler.
func New ¶
New initializes a Server-Sent Events (SSE) handler, with an optional configuration. If the provided configuration is nil, then it uses the default configuration.
Example ¶
package main import ( "fmt" "net/http" "time" "github.com/softwarespot/sse" ) func main() { // Use the default configuration h := sse.New[int](nil) defer h.Close() go func() { var evt int for { fmt.Println("sse handler: broadcast event", h.Broadcast(evt)) evt++ time.Sleep(64 * time.Millisecond) } }() http.Handle("/events", h) http.ListenAndServe(":3000", nil) }
Output:
func (*Handler[T]) Broadcast ¶
Broadcast broadcasts one or more events to all the connected clients. It returns an error if the handler is closed.
func (*Handler[T]) Close ¶
Close closes the Server-Sent Events (SSE) handler. It waits for all clients to complete/close, with a timeout defined in the configuration.
Click to show internal directories.
Click to hide internal directories.