Chatto, from the inside
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
Chatto is one executable file. It contains the server, the web app, and the data store. You give it a port and a data directory.
One programEach change is a fact: a user posted a message, a member joined a room. Chatto appends each fact to one ordered log, the EVT stream. This log is the source of truth.
Projections read the log and build the current state in memory: the rooms, the members, the timelines. Chatto can discard a projection and build it again from the log.
ProjectionsEach connected app keeps one WebSocket open. The server sends each new event that the user is permitted to see.
How changes arrive liveFollow one message
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.
Step 1 of 6
You press Enter. The app calls the public API of the server. The API uses ConnectRPC with protobuf messages.
How changes arrive liveStep 2 of 6
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.
Who may do whatStep 3 of 6
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.
Encryption and real deletionStep 4 of 6
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.
Step 5 of 6
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.
ProjectionsStep 6 of 6
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.
How changes arrive liveWhere data goes
Chatto has no SQL database. All data is in streams and key-value buckets of NATS JetStream. Each one has one purpose.
EVT
The durable history of the domain: every fact.
RUNTIME_STATE
Operational records of which only the latest value is important.
ENCRYPTION_KEYS
The keys that protect the keys of each user.
SERVER_ASSETS
Files and images. A server can use S3 as an alternative.
JOBS
The facts of durable background jobs.
NOTIFICATIONS
The lifecycle of each notification.
LOG
Operational records that the server retains.
PROJECTION_SNAPSHOTS
Derived data that Chatto can discard, as is ASSET_CACHE.
MEMORY_CACHE
Volatile state that the server processes share.
More than one server
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.
More than one processDeep dives
Each chapter explains one part of Chatto, with the decisions behind it.
Go deeper
This page follows the system map of the repository, which also lists each runtime component and its source file.