Chatto, from the inside

Whatto
Chatto?

How Chatto works, and why.

Read the short version first. Then follow one message through the server. Each statement links to the record that decided it.

The short version Browse the records

The short version

Four ideas explain most of Chatto

Follow one message

From the Enter key to another screen

A message goes through six steps. Almost every other change in Chatto, such as a reaction or a new room, goes through the same steps.

  1. Step 1 of 6

    The app sends a request

    You press Enter. The app calls the public API of the server. The API uses ConnectRPC with protobuf messages.

    ADR-042ADR-044

    How changes arrive live
  2. Step 2 of 6

    The server checks the request

    The API handler identifies the user. Then the core checks that the user is a member of the room and has the permission to post.

    ADR-087ADR-052ADR-115

    Who may do what
  3. Step 3 of 6

    The text is encrypted

    Chatto encrypts the message text with a key that belongs to the author. When the account is deleted, Chatto destroys the key. After that, no one can read the text.

    ADR-007

    Encryption and real deletion
  4. Step 4 of 6

    The message becomes a fact

    Chatto appends the message to the EVT stream. The append succeeds only if no other fact changed the room during the check. If one did, Chatto does the check and the append again.

    ADR-016ADR-034ADR-008

    The log of facts
  5. Step 5 of 6

    Projections apply the fact

    Each projection reads the new fact and updates its state in memory. The request waits until the projections have applied the fact. Thus the author always sees their own message.

    ADR-033ADR-088

    Projections
  6. Step 6 of 6

    Everyone gets the message

    JetStream publishes each committed fact again on a live subject. One hub in each server process receives it. For each connection, the server checks that the user is permitted to see the event, and then sends it through the WebSocket.

    ADR-049ADR-093FDR-045

    How changes arrive live

Where data goes

NATS JetStream is the database

Chatto has no SQL database. All data is in streams and key-value buckets of NATS JetStream. Each one has one purpose.

More than one server

Each process builds its own state

You can run more than one Chatto process on the same NATS cluster. Each process reads the same log and builds its own projections. No process is the leader.

A rule that must hold across all processes uses the atomic operations of JetStream. It does not use a lock in the memory of one process.

ADR-073ADR-115

More than one process

Deep dives

Now go into detail

Each chapter explains one part of Chatto, with the decisions behind it.

  1. One program How one executable file contains the Chatto server, its data store, and the web app. 4 min
  2. The log of facts How Chatto records each change as a fact in one ordered log, and how an append stays safe. 5 min
  3. Projections How Chatto computes its current state from the log, and how a writer sees its own write. 5 min
  4. How changes arrive live How a committed change reaches each connected app through one WebSocket, with a permission check for each event. 5 min
  5. Who may do what How roles, scopes, the owner override, and membership decide each action, and how a command checks them safely. 5 min
  6. Encryption and real deletion How keys for each user protect message text and personal data, and how account deletion destroys those keys. 5 min
  7. Work after the request How Chatto runs slow work as durable background jobs that survive a restart and stay visible when they fail. 5 min
  8. The client How one TypeScript client keeps server data current for the web app, for bots, and for other frontends. 5 min
  9. More than one process How several Chatto processes share one NATS cluster and stay correct without a leader or an in-process lock. 5 min

Go deeper

The reasons are written down

This page follows the system map of the repository, which also lists each runtime component and its source file.

Esc