live

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package live is the in-process pub-sub for SPA live updates.

Tiny WebSocket fan-out: any number of subscribed browsers receive `created` / `destroyed` events the moment the store mutates.

Wire format is intentionally minimal — frontend consumes plain JSON frames, no subscription handshake protocol:

{ "type": "created",   "message": { ... MessageSummary ... } }
{ "type": "destroyed", "id":      "<message-id>" }

Concurrency: each subscriber gets a bounded queue (chan []byte) and a writer goroutine that drains the queue and calls Send. Broadcasts do a non-blocking send to each queue; if a subscriber's queue is full, the hub drops that subscriber rather than stalling the broadcast. This is the textbook fan-out pattern — one slow consumer can't hold up the others.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hub

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

Hub owns the subscriber set and broadcasts events to all of them. Safe for concurrent use.

func NewHub

func NewHub() *Hub

func (*Hub) BroadcastCreated

func (h *Hub) BroadcastCreated(summaryJSON json.RawMessage)

BroadcastCreated fans out a "created" event with the given JSON- encoded MessageSummary payload.

func (*Hub) BroadcastDestroyed

func (h *Hub) BroadcastDestroyed(id string)

BroadcastDestroyed fans out a "destroyed" event for the given ID.

func (*Hub) Count

func (h *Hub) Count() int

Count returns the current subscriber count (useful for tests + a future ops endpoint).

func (*Hub) Subscribe

func (h *Hub) Subscribe(s Subscriber)

Subscribe adds a subscriber and starts its writer goroutine. Caller is responsible for calling Unsubscribe (typically deferred at the end of the WebSocket loop). Calling Subscribe twice with the same Subscriber replaces the prior worker.

func (*Hub) Unsubscribe

func (h *Hub) Unsubscribe(s Subscriber)

Unsubscribe removes a subscriber. The writer goroutine exits and calls Close on the Subscriber. Idempotent.

type Subscriber

type Subscriber interface {
	Send(msg []byte) error
	Close() error
}

Subscriber is the destination for events. The Hub never blocks on a slow subscriber: if Send returns an error, or if the subscriber's queue overflows, the subscriber is dropped from the broadcast set and Close is called.

Jump to

Keyboard shortcuts

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