Skip to main content

Module realtime

Module realtime 

Source
Expand description

v2 realtime — the authors-based live subscription + dispatch.

v2 addresses planes by their group pubkey (CORD-01), not v1’s #z tag, so the subscription is {kinds:[1059,21059], authors:[…plane pubkeys…]}. A received event is routed to the owning community and handed to the shared inbound::dispatch_wrap, which fires the protocol-agnostic InboundEventHandler the SDK’s on_message consumes.

The kind-1059 dispatch rule (CLAUDE.md A4): the listen loop tries this v2 path for events on the v2 subscription; DM gift wraps and 3313 Direct Invites (both #p=me) stay on the DM subscription — the author-set here never includes an identity key, so the two never collide.

Functions§

clear
Clear the v2 realtime state on a session reset (called from swap_session alongside v1’s clear), so a stale sub id / author-set can’t leak across accounts.
dispatch_event
Route an arriving v2 wrap: find the held community whose plane it opens under and fire the matching handler callback (via the shared bridge). Persistence to the local DB is deferred (bots deliver via the callback; GUI history is v1 for now). session gates against a mid-flight account swap.
enqueue_follow
Queue a follow for id — NON-BLOCKING + coalesced. A burst (or a junk-wrap flood) collapses to at most one queued + one running follow per community. A no-op if no worker is running (no live listen()). Callers: dispatch, boot/reconnect catch-up, manual sync — none of them block or touch a lock for longer than the enqueue.
follow_worker_running
Whether a live follow worker is draining the queue (a listen() is running). Headless callers use this to run a follow inline instead of enqueueing into the void.
load_held_v2
Load every locally-held, LIVE v2 community (dispatching each id by its stored protocol). The realtime layer folds these into the subscription + routing, so excluding a DISSOLVED community here is what enforces CORD-02 §9 on the receive side: its chat/control/rekey planes stop being subscribed and an arriving wrap for it is NotOurs (dropped, never honored). Held keys still open old history through the explicit read paths — this only stops NEW events.
plane_authors
Every plane pubkey a set of v2 communities publishes under that inbound::dispatch_wrap handles — the subscription author-set. Per community: the guestbook, the control plane, and each channel’s current Chat-Plane address. Pure + deterministic (deduped, sorted) — the testable core.
poolwide_subscription_id
refresh_subscription
Refresh the v2 subscription for the held communities: register {kinds:[1059,21059], authors:[…]} on their relays (targeted + pool-wide, mirroring v1). Idempotent on an unchanged author-set.
spawn_follow_worker
Spawn the single follow worker for this session. Installs the queue sender and drains it, running one combined follow per community at a time. Replacing the sender (a re-listen()) or clear (a swap) closes the old channel so the old worker exits; the captured SessionGuard also stops it. Idempotent per session.
subscription_id