Documentation
¶
Overview ¶
Package realtime is a publish/subscribe hub for pushing updates to browsers over server-sent events or WebSockets:
var hub = realtime.NewHub[StatusUpdate]()
// Wherever something changes:
hub.Publish("monitors", update)
// In an endpoint:
func GET(ctx *kit.Ctx) error {
return realtime.Stream(ctx, hub, "monitors")
}
The hub is in memory, so it reaches the browsers connected to this process. Apps running several instances can implement Broker on top of Redis, NATS or Postgres LISTEN/NOTIFY and pass it to NewHubWith.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Broker ¶
type Broker interface {
// Publish delivers msg to the topic's subscribers.
Publish(ctx context.Context, topic string, msg []byte) error
// Subscribe calls fn for each message on topic until ctx is canceled.
// fn must not block for long.
Subscribe(ctx context.Context, topic string, fn func(msg []byte)) error
}
Broker carries encoded messages between publishers and subscribers. The default broker is in memory.
type Hub ¶
type Hub[T any] struct { // Buffer is how many messages a slow subscriber may fall behind before // messages to it are dropped. The default is 64. Buffer int // contains filtered or unexported fields }
Hub fans out messages of type T to subscribers of a topic. It is safe for concurrent use.
func (*Hub[T]) PublishContext ¶
PublishContext is Publish with a context for the broker.
type MemoryBroker ¶
type MemoryBroker struct {
// contains filtered or unexported fields
}
MemoryBroker delivers messages within one process.
func NewMemoryBroker ¶
func NewMemoryBroker() *MemoryBroker
NewMemoryBroker returns an empty in-memory broker.
func (*MemoryBroker) Subscribers ¶
func (b *MemoryBroker) Subscribers(topic string) int
Subscribers returns how many subscriptions topic has, for metrics and tests.