Server-Sent Events (SSE) utilities for Go. Server broker and client, zero dependencies
go get github.com/philiprehberger/go-sseutilServer-Sent Events (SSE) utilities for Go. Server broker and client, zero dependencies
go get github.com/philiprehberger/go-sseutil
package main
import (
"net/http"
"time"
"github.com/philiprehberger/go-sseutil"
)
func main() {
broker := sseutil.NewBroker(sseutil.WithKeepAlive(15 * time.Second))
http.Handle("/events", broker.Handler())
go func() {
for {
broker.Broadcast(sseutil.Event{
Event: "tick",
Data: "hello",
})
time.Sleep(1 * time.Second)
}
}()
http.ListenAndServe(":8080", nil)
}
package main
import (
"context"
"fmt"
"github.com/philiprehberger/go-sseutil"
)
func main() {
ctx := context.Background()
stream, err := sseutil.Connect(ctx, "http://localhost:8080/events")
if err != nil {
panic(err)
}
defer stream.Close()
for event := range stream.Events() {
fmt.Printf("Event: %s, Data: %s\n", event.Event, event.Data)
}
}
// Send JSON to a specific client.
type Message struct {
Text string `json:"text"`
From string `json:"from"`
}
err := broker.SendJSON("client-1", "chat", Message{Text: "hi", From: "alice"})
// Sends: event: chat\ndata: {"text":"hi","from":"alice"}\n\n
// Broadcast JSON to all clients.
err = broker.BroadcastJSON("update", map[string]any{"status": "ok"})
// Subscribe a client to topics.
broker.Subscribe("client-1", "sports", "news")
// Publish to a topic — only subscribed clients receive the event.
broker.PublishTopic("sports", sseutil.Event{Event: "score", Data: "3-2"})
broker.OnConnect(func(clientID string) {
log.Printf("client connected: %s", clientID)
})
broker.OnDisconnect(func(clientID string) {
log.Printf("client disconnected: %s", clientID)
})
event := sseutil.Event{
ID: "1",
Event: "update",
Data: "payload data",
Retry: 5000,
}
fmt.Print(event.String())
// Output:
// id: 1
// event: update
// data: payload data
// retry: 5000
| Type / Function | Description |
|---|---|
Event | SSE event with ID, Event, Data, and Retry fields |
Event.Bytes() | Encode event to SSE wire format |
Event.String() | String representation of SSE event |
Broker | Manages SSE client connections |
NewBroker(opts...) | Create a new broker with options |
WithKeepAlive(d) | Set keep-alive interval (default 30s) |
Broker.Handler() | HTTP handler for SSE endpoint |
Broker.Broadcast(e) | Send event to all clients |
Broker.Send(id, e) | Send event to a specific client |
Broker.SendJSON(id, event, data) | Marshal data to JSON and send to a client |
Broker.BroadcastJSON(event, data) | Marshal data to JSON and broadcast to all |
Broker.Subscribe(id, topics...) | Subscribe a client to named topics |
Broker.PublishTopic(topic, e) | Send event to all clients subscribed to topic |
Broker.OnConnect(fn) | Set callback for client connections |
Broker.OnDisconnect(fn) | Set callback for client disconnections |
Broker.ClientCount() | Number of connected clients |
Connect(ctx, url) | Connect to an SSE endpoint |
Stream.Events() | Channel of received events |
Stream.Close() | Close the connection |
Stream.LastEventID() | Last received event ID |
go test ./...
go vet ./...
If you find this project useful: