ws

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ws serves live values over WebSocket (spec 9.3): subscriptions per client, a per-client diff against what was sent, pushes coalesced into one frame per tick.

Index

Constants

View Source
const (
	MaxClients = 256
	MaxKeys    = 256 // per frame, in both directions
	MaxMessage = 1 << 20
)

Limits (spec 9.3).

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

type Bus interface {
	Subscribe(size int, policy eventbus.Overflow) *eventbus.Subscription
	Unsubscribe(s *eventbus.Subscription)
}

Bus is the part of the event bus the hub subscribes to.

type Cache

type Cache interface {
	Get(key string) (livecache.Value, bool)
}

Cache is the part of the live cache the hub reads.

type Handler

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

Handler upgrades GET /ws and runs one connection on the current hub. The upgrader keeps the library's same-origin check (Origin host == Host).

func NewHandler

func NewHandler(slot *util.Slot[*Hub], log zerolog.Logger) (*Handler, error)

NewHandler builds the handler for the hub published in slot.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP upgrades the connection and serves it until either side closes.

type Hub

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

Hub is the restartable component that owns all clients. The HTTP handler reaches the current instance through a util.Slot, because a restart replaces it.

func NewHub

func NewHub(s *site.Site, cache Cache, bus Bus, clock util.Clock, slot *util.Slot[*Hub],
) (*Hub, error)

NewHub builds a hub for the live keys of site s and publishes it in slot on Start.

func (*Hub) LastBeat

func (h *Hub) LastBeat() time.Time

LastBeat returns the time of the last loop iteration.

func (*Hub) Start

func (h *Hub) Start(ctx context.Context) error

Start runs the hub loop and publishes the hub for new connections.

func (*Hub) State

func (h *Hub) State() health.State

State is always Healthy: a broken client is that client's problem.

func (*Hub) Stop

func (h *Hub) Stop() error

Stop withdraws the hub from the slot, disconnects every client and waits for the loop.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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