Two-tier real-time: entity-upsert streams content directly to active viewers via SSE, while poke signals keep Replicache in sync for everything else.
The chat or chart workflow (running on the server) streams AI-generated content and writes each chunk to Postgres as ChatMessageContent rows.
After each write, the agent fires pg_notify with the IDs of the rows it just inserted — keeping the signal lightweight while telling downstream exactly what changed.
PostgreSQL's built-in NOTIFY broadcasts a lightweight signal to all listening server processes. The payload carries only { contentIds } — no actual row data.
This decouples the write path from the read path: the agent doesn't need to know who's listening or how many viewers are connected. PubSub handles the fan-out of the signal, not the data.
Receives signals and debounces them in a 16ms window — coalescing rapid-fire token writes into a single database read.
Fetches only the delta: SELECT WHERE id IN (...) using the content IDs from the signal. One query serves all connected viewers on that topic, regardless of how many there are.
This is the key efficiency: N viewers watching the same chat share one DB query, not N queries.
Each browser tab holds one SSE connection that multiplexes all its topic subscriptions (org, user, chat, chart). No per-entity connections needed.
The delta rows are serialized as entity-upsert ops and pushed over the SSE pipe with brotli compression (quality 4). The client applies them directly to React Query caches — bypassing Replicache for instant UI updates.
Org-level state changes go through a separate poke → Replicache pull slow path for consistency.
Each browser tab opens one SSE connection, multiplexing all topics over a single HTTP stream:
StreamProvider opens GET /api/sse/stream?topics=org:…,user:… — the server validates access to each topic, then holds the response open.useStreamSubscription adds topics to the set. The client closes and reopens the EventSource with the updated ?topics= query param.TopicManager does PG LISTEN signal:chat:… when the first client subscribes to a topic, and UNLISTEN when the last disconnects.There's no explicit subscribe/unsubscribe API — topic changes always trigger a clean reconnect with the full topic list.
steps 1 → 2
step 3 → 4
inside one browser tab