realtime

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 4 Imported by: 0

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

func Stream

func Stream[T any](ctx *kit.Ctx, h *Hub[T], topic string) error

Stream responds to an endpoint request with a server-sent event stream of topic's messages, each as an event named after the topic. Read it in the browser with stream() from @ternkit/tern.

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 NewHub

func NewHub[T any]() *Hub[T]

NewHub returns a hub backed by an in-memory broker.

func NewHubWith

func NewHubWith[T any](b Broker) *Hub[T]

NewHubWith returns a hub backed by b.

func (*Hub[T]) Publish

func (h *Hub[T]) Publish(topic string, v T) error

Publish sends v to everyone subscribed to topic.

func (*Hub[T]) PublishContext

func (h *Hub[T]) PublishContext(ctx context.Context, topic string, v T) error

PublishContext is Publish with a context for the broker.

func (*Hub[T]) Subscribe

func (h *Hub[T]) Subscribe(ctx context.Context, topic string) (<-chan T, error)

Subscribe returns a channel of the messages published to topic until ctx is canceled, when the channel is closed. A subscriber that falls more than Buffer messages behind misses messages rather than slowing down publishers.

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) Publish

func (b *MemoryBroker) Publish(_ context.Context, topic string, msg []byte) error

Publish implements Broker.

func (*MemoryBroker) Subscribe

func (b *MemoryBroker) Subscribe(ctx context.Context, topic string, fn func([]byte)) error

Subscribe implements Broker.

func (*MemoryBroker) Subscribers

func (b *MemoryBroker) Subscribers(topic string) int

Subscribers returns how many subscriptions topic has, for metrics and tests.

Jump to

Keyboard shortcuts

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