Documentation
¶
Overview ¶
Package live fans per-user events from Postgres NOTIFY out to WebSocket clients.
Index ¶
Constants ¶
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).
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.