Help us shape the future of Go! Take the 2026 Go Developer Survey

sse

package
v0.36.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 10 Imported by: 2

Documentation

Overview

Package sse streams server-sent events (text/event-stream).

return sse.Serve(w, r, func(s *sse.Stream) error {
	return s.Send("message", "<li>new post</li>")
})

Serve flushes every event, sends heartbeat comments, lifts the server's write timeout for the stream and ends when the client disconnects, the callback returns or the server shuts down. A Hub fans events out to many streams, in memory or through a Broker (drivers/redis) across instances.

See https://github.com/AlexGalhardo/vukem/blob/main/docs/frontend.md.

Example
package main

import (
	"context"
	"net/http"

	"github.com/AlexGalhardo/vukem/sse"
)

func main() {
	hub := sse.NewHub(nil) // or sse.NewHub(redisClient) across instances

	http.HandleFunc("GET /events", func(w http.ResponseWriter, r *http.Request) {
		_ = sse.Serve(w, r, func(s *sse.Stream) error { return hub.Stream(s, "posts") })
	})

	// Elsewhere, e.g. after creating a post:
	_ = hub.Publish(context.Background(), "posts", sse.Event{Event: "created", Data: "<li>New post</li>"})
}

Index

Examples

Constants

View Source
const DefaultHeartbeat = 15 * time.Second

DefaultHeartbeat is the interval of keep-alive comments.

Variables

This section is empty.

Functions

func Serve

func Serve(w http.ResponseWriter, r *http.Request, fn func(*Stream) error, opts ...Option) error

Serve streams events to the client of r with fn until fn returns, the client goes away or the server shuts down (Stream.Context is then done).

func ShutdownSignal added in v0.19.0

func ShutdownSignal(ctx context.Context) <-chan struct{}

ShutdownSignal returns the channel installed by WithShutdown, or nil when ctx has none. Other long-lived connections (the WebSocket driver) end on the same signal.

func WithShutdown

func WithShutdown(ctx context.Context, stop <-chan struct{}) context.Context

WithShutdown makes streams served with ctx end when stop is closed; the app's HTTP server does this so shutdown does not wait for open streams.

Types

type Broker

type Broker interface {
	Publish(ctx context.Context, channel string, payload []byte) error
	Subscribe(ctx context.Context, channels ...string) (<-chan []byte, error)
}

Broker carries events between app instances. Two implementations ship with the framework: *redis.Client from drivers/redis (pub/sub) and *postgres.Broker from drivers/postgres (LISTEN/NOTIFY, for an app that has PostgreSQL and no Redis; it refuses a payload over 7999 bytes, the NOTIFY limit). Subscribe must end the subscription and close the returned channel once ctx is done: the Hub cancels it when a topic loses its last local subscriber.

type Event

type Event struct {
	// ID becomes the client's Last-Event-ID on reconnect.
	ID string `json:"id,omitempty"`
	// Event is the event type ("message" when empty).
	Event string `json:"event,omitempty"`
	// Data is the payload; newlines become several data lines.
	Data string `json:"data"`
	// Retry tells the client how long to wait before reconnecting.
	Retry time.Duration `json:"retry,omitempty"`
}

Event is one server-sent event.

type Hub

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

Hub fans events published on topics out to subscribed streams. Without a Broker it works within one process; with one, every instance receives every event. A topic holds one broker subscription while it has local subscribers and releases it when the last one leaves.

func NewHub

func NewHub(broker Broker) *Hub

NewHub returns a hub; broker may be nil for a single instance.

func (*Hub) Close

func (h *Hub) Close()

Close ends every subscription; later subscriptions end immediately.

func (*Hub) Publish

func (h *Hub) Publish(ctx context.Context, topic string, ev Event) error

Publish sends ev to every subscriber of topic.

func (*Hub) Stream

func (h *Hub) Stream(s *Stream, topics ...string) error

Stream sends the events of topics to s until the client disconnects, the server shuts down or the hub closes.

func (*Hub) Subscribe

func (h *Hub) Subscribe(ctx context.Context, topics ...string) (<-chan Event, func())

Subscribe returns a channel of the events of topics and a function that unsubscribes. The channel closes on unsubscribe, ctx cancellation, Close, or when the subscriber falls too far behind.

func (*Hub) Subscribers

func (h *Hub) Subscribers(topic string) int

Subscribers reports how many subscribers topic has.

type Option

type Option func(*options)

Option configures Serve.

func Heartbeat

func Heartbeat(d time.Duration) Option

Heartbeat sets the keep-alive interval (zero or negative disables it).

type Stream

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

Stream writes events to one client; it is safe for concurrent use.

func (*Stream) Context

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

Context is done when the client disconnects or the server shuts down.

func (*Stream) LastEventID

func (s *Stream) LastEventID() string

LastEventID is the Last-Event-ID header of a reconnecting client.

func (*Stream) Send

func (s *Stream) Send(event, data string) error

Send writes an event with a type and data.

func (*Stream) SendEvent

func (s *Stream) SendEvent(ev Event) error

SendEvent writes ev and flushes it.

func (*Stream) SendJSON

func (s *Stream) SendJSON(event string, v any) error

SendJSON writes an event whose data is v encoded as JSON.

Jump to

Keyboard shortcuts

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