The frontend side of rooms: RoomClient keeps, for each room a page joins
or watches, a reactive RoomState — its members and their state, the
caller's own seat — over the WS-only room_* verbs and the room_*
notifications the server pushes (actions/room_action_specs.ts).
Desired state, converged one request at a time
RoomState.join, leave, watch, unwatch, and set_state record what
the page wants; one runner per room then sends whatever requests close the
gap, one at a time. The server dispatches a socket's requests concurrently,
so a leave sent right after a join could otherwise reach the registry first
(and refuse the join as superseded), and two state sets could land out of
order. A burst of calls converges: join, leave, join while the first join is
in flight sends that one join, whose seat stands; a leave while seated is
sent before the next join. While the socket is down nothing is sent and the
wants wait for it. RoomState.send is the exception: it goes out at once,
since a caller that cares about the order of its sends (the WebRTC mesh,
rtc/rtc_mesh.svelte.ts) serializes them itself.
Every verb runs untracked, so a page may call join or watch from an
$effect (with leave or unwatch in its teardown) without the effect
depending on the room state the verb reads and writes.
Listeners are isolated: one that throws is logged and the others still
hear the event, and the join, replay, or teardown that called it carries on.
seq
Every member event carries its room's seq, and a join or watch answers the
room as of a seq. Events can arrive before that answer, so while a room's
first answer is outstanding they are buffered; the answer's members are
applied, then the buffer replays, dropping every event at or below the
answer's seq. Once a room is live, an event at or below the last applied
seq is dropped, so a connection that both watches a room and sits in it,
which the server tells each event once, applies it once. A later answer
whose seq is behind the live stream is not applied over it. A message
(which reaches only a seat) that arrives on a live watch while the join is
in flight is held, and delivered once the seat is taken.
Seats lost, and reconnects
room_seat_lost ends a seat the server took: moved (a newer join of the
same actor took over, moved_to naming its room), evicted, or
room_closed. None is rejoined. A socket that closes loses every seat and
watch with it, and the client tells them apart: a seat lost to the
disconnect is rejoined on the next connection, naming the seat it held
(rejoin_of), and a refusal as room_seat_moved — the actor took a seat
elsewhere meanwhile — ends it as moved, so two tabs never trade a seat back
and forth. A rejoin refused rate_limited is retried after the refusal's
retry_after, still naming the seat, up to ROOM_REJOIN_RATE_LIMITED_RETRIES_MAX
times. Watches are taken again.
The client learns of a new connection through RoomRpc.connection, an
identity that changes each time the socket opens. The client follows it on
its own: its constructor makes an $effect.root whose effect reads it and
calls sync_connection, so a close and a reopen are seen even when they
land in one batch, and dispose ends it. On the server the compiler makes
$effect.root a no-op, and nothing follows; sync_connection stays public
for that case and for tests, and runs on its own before each request and
notification.
const rooms = new RoomClient(create_room_rpc(socket));Notifications reach the client through receive, from the RPC client's
lookup_action_handler for the 'receive' phase (with
room_notification_action_specs among the RPC client's specs):
lookup_action_handler: (method, phase) =>
phase === 'receive' && is_room_notification_method(method)
? (event) => rooms.receive(method, event.data.input)
: undefineddispose leaves every seat and drops every watch, for a clean teardown.