Deep dives Chapter 4 of 9

How changes arrive live

How a committed change reaches each connected app through one WebSocket, with a permission check for each event.

5 min Checked against its records on 2026-12-01 Source file

The app talks to the server in two ways. It sends requests through the public API. It receives changes through one WebSocket, which is a network connection that stays open so that the server can send data at any time.

When Chatto commits a fact, such as a new message, each server process gets a copy of it. The process waits until its own state includes the fact. Then, for each connection, it checks that the user is permitted to see the event, and sends a public event.

Two transports, one contract

Protobuf files define the public API (ADR-042). Protobuf is a compact binary format with a schema. The app sends requests with ConnectRPC, a protocol that calls protobuf-defined services over HTTP. Reads, commands, and admin operations use it.

Method names follow one vocabulary, such as List, Get, Create, and Update (ADR-044). The bundled web app and a bot use the same API.

Live delivery uses a WebSocket with binary protobuf frames. Chatto does not use ConnectRPC streaming for it. A long-lived stream needs control of resume, heartbeat, reconnect, and backpressure (ADR-042).

A browser sends its session cookie with the HTTP request that opens the WebSocket, so the server knows the user before the connection starts (ADR-017). A bot puts a bearer token in its subscription message. For a cookie or bearer credential, the server checks it again once each minute.

Two feeds inside the server

Chatto contains NATS, a messaging system. A message in NATS has a subject, which is its address. JetStream is the storage layer of NATS. It keeps the log of facts in the EVT stream.

JetStream publishes each committed fact again on a subject below live.evt.>. The > is a wildcard for all subjects below that root. Only the server reads this feed.

Some activity is not a fact. A typing indicator is useful only at that moment, so Chatto does not store it (FDR-010). The server publishes such signals on live.sync.> with plain NATS, and they are lost if nobody listens (ADR-012).

The two feeds use two different message types: Event for stored facts and PubSubEvent for transient signals. Thus a transient signal cannot go into EVT through the normal publisher (ADR-094).

One hub, and one check for each user

Each server process has one hub with one NATS subscription to live.>, which covers the two feeds (ADR-049). The hub does the shared work one time, not one time for each connection:

  1. It reads the subject and discards private facts, such as encrypted message bodies, before it decodes them.
  2. It decodes each remaining event one time.
  3. For a stored fact, it waits one time until the local projections have applied the fact.
  4. It authorizes the event for each connected user.
  5. It puts the same decoded event in the queue of each session of that user.

Each session queue has a limit for the number of events and for their size. The hub disconnects a session that exceeds a limit, and the other sessions continue (ADR-049).

The wait in step 3 is necessary because authorization reads the projections. A new member must be in the membership state before the server decides who gets the first message.

Message, asset, and typing events go only to room members who also have the permission to read messages (FDR-045). Assume that Ana posts in a room. Ben is a member with message.read, so his app gets the event. Cleo is not a member, so her connection gets nothing.

The server uses the permissions that the user has at delivery time. This rule applies to live delivery, to replay after a reconnect, and to snapshots (ADR-091). Access that a user had earlier gives no access now.

The public event union

The app never gets the stored bytes. The server maps each authorized event to a new RealtimeEvent (ADR-093). This message contains:

  • the event ID, the source time, and the ID of the user who caused the event;
  • one payload from an explicit list of public event types;
  • an optional cursor, which the next section explains.

If an internal event has no public type, the server omits it. Several internal facts can map to one public event. For example, all profile facts become UserProfileChangedEvent.

The cursor is not part of the payload. Thus a client can ignore an event type that it does not know and still keep the cursor.

The frame set is small (ADR-094):

client to server:  RealtimeSubscribe (one time)
server to client:  snapshot | event | caught_up | heartbeat | close

Resume after a reconnect

Each replayable event has a cursor: an encrypted token that the client must not read (ADR-091). A client that reconnects sends its last cursor. The server sends the authorized events after that position, and then continues live.

Resume repairs a short gap. A cursor expires 15 minutes after the server issues it. The server also limits the range of the log that it reads and the number of events that it sends.

When the cursor is missing, expired, or too expensive, the server uses the fallback that the subscription selected:

  • SNAPSHOT: the server sends the current state in one frame. The bundled app uses this.
  • LIVE_ONLY: the server starts at the current position and sends no past events.

The caught_up frame reports the result: RESUMED, SNAPSHOT, or LIVE_ONLY. The server does not create a JetStream consumer for a client. It reads the range of EVT directly (ADR-091).

Typing and other transient signals have no cursor, and the server does not replay them. A client can get an event two times, so it uses the event ID to discard the copy (FDR-045).

Read the code

The records behind this chapter

Esc