live

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: AGPL-3.0 Imports: 10 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 live fans per-user events from Postgres NOTIFY out to WebSocket clients.

Index

Constants

View Source
const (
	ChannelDerived = "braid_derived"
	ChannelSynced  = "braid_synced"
)

NOTIFY channels carrying Event payloads.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	UserID    string `json:"u"`
	Type      string `json:"t"` // "session_updated" | "reindex" | "resync" on braid_derived; "device_synced" on braid_synced
	SessionID string `json:"s,omitempty"`
	DeviceID  string `json:"d,omitempty"`
	State     string `json:"state,omitempty"` // reindex: "started" | "done"
}

Event is the NOTIFY payload; Message is what clients receive. It never carries data, only ids.

type Hub

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

Hub maps users to their connections and fans events out to the owner only.

func NewHub

func NewHub(listen ListenFunc, o Options) *Hub

NewHub creates a hub; call Run to start listening.

func (*Hub) Handler

func (h *Hub) Handler(public *url.URL, check func(*http.Request) (webauth.Principal, bool, error)) http.Handler

Handler serves the WebSocket. It authenticates itself (session cookie, exact same-origin Origin).

func (*Hub) Publish

func (h *Hub) Publish(userID string, m Message)

Publish queues m for every connection of userID, and for nobody else.

func (*Hub) Ready

func (h *Hub) Ready() <-chan struct{}

Ready is closed once both LISTENs are active.

func (*Hub) Run

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

Run listens on both channels and fans events out until ctx is done.

type ListenFunc

type ListenFunc func(ctx context.Context, channel string, ready func(), onNotify func(payload string)) error

ListenFunc is (*db.DB).Listen.

type Message

type Message struct {
	Type      string `json:"type"` // hello | ping | session_updated | device_synced | reindex | resync
	Protocol  int    `json:"protocol,omitempty"`
	SessionID string `json:"session_id,omitempty"`
	DeviceID  string `json:"device_id,omitempty"`
	State     string `json:"state,omitempty"`
}

Message is one server-to-client text frame (JSON). It carries ids only, never data.

type Options

type Options struct {
	MaxPerUser        int           // default 10; more -> 429
	MaxTotal          int           // default 500
	QueueSize         int           // default 32; overflow drops the queue and sends one resync
	PingEvery         time.Duration // default 25s
	RecheckEvery      time.Duration // default 1m: re-validate the cookie, close with 4401 if gone
	ConnectsPerMinute int           // default 30 per user
}

Options tunes the hub; zero values take the defaults.

Jump to

Keyboard shortcuts

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