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 (*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 ¶
BroadcastDestroyed fans out a "destroyed" event for the given ID.
func (*Hub) Count ¶
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 ¶
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.