Deep dives Chapter 8 of 9

The client

How one TypeScript client keeps server data current for the web app, for bots, and for other frontends.

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

Chatto has one client: the TypeScript package @chatto/client. The bundled web app uses it, the example bots use it, and a third-party frontend can use it. The package connects to one or more servers, keeps their data current, and exposes that data as reactive state.

The package does not depend on a UI framework. The web app is a SvelteKit application on top of the package. The app adds what only a user interface needs: components, routes, translated text, and UI state.

One client for every host

ADR-111 puts the complete client into one package. Thus, a bot gets the same recovery and privacy rules as the web app.

createClient() makes a client instance. An instance is isolated: it owns its servers, their sessions, and their background work. Each server of a client is one Server object with its data, its events, and its requests. A bot and the web app use the same type.

The package has its own small signal library, so that data can be reactive without Svelte. Only the optional module @chatto/client/svelte imports Svelte. It connects the signals to Svelte, so a component can read store.status like normal Svelte state.

In the repository, the package exports its TypeScript source, and each consumer compiles it with its own tool (ADR-116). The npm release contains compiled JavaScript.

Server data from a snapshot and events

The client keeps a copy of the current state of each server: the rooms, the notifications, the user profiles, and more. The glossary calls this copy the Client Projection. The client builds it in two steps:

  1. It opens the realtime connection and receives a snapshot of the current state.
  2. It applies each later realtime event to that state. For some events, it also reads the changed resource from the API.

For example, a member joins a room. The server sends an event. The client adds the member ID to the member list of that room and reads the profile of the member. All views that show this user read one shared store of public profiles for each connection (ADR-101). Thus, a name cannot be different in two places.

Some data must disappear at a known moment: the user loses access to a room, an account is deleted, or the session ends. The client clears its own data at each such boundary and emits a boundary event. A host that keeps a copy of server data clears the copy at that event (ADR-111).

The web app on top

The web app is a single-page application. It is built to static files and embedded in the server executable (ADR-018). It creates exactly one client. A component reads server data directly from the stores of the client.

The app keeps its own state next to each server: sidebar navigation, search sessions, scroll positions, and the voice call. This state is not in the package, because a bot does not need it.

Not all screens need live data. An admin member list or a permission table is a bounded read that the app shows as it was when loaded. For these reads, the app uses the TanStack Query library, which removes duplicate requests and keeps recent results in memory (ADR-062). The app clears this cache at the boundary events of the client.

Optimistic UI with provisional patches

Some actions must feel instant. When you add a reaction, the app shows it before the server answers. This is optimistic UI.

Chatto does not change the loaded server data in place for this. It puts a provisional patch on top of it (ADR-048):

  • Each pending action has a key for the smallest part of state that it owns, such as one message and one emoji.
  • Each action gets a token. When the answer arrives late and a newer action has the same key, the old answer changes nothing.
  • A failed action removes only its own patch. It does not restore the full message row, which can have changed for a different reason.
  • When the client reads the row from the server again, the server data is correct, and the pending patches of that row are cleared.

Several servers and device storage

One app can connect to several Chatto servers at the same time (ADR-025). Each server has its own session, its own stores, and its own permissions.

The URL selects the active server. The path segment - means the server that served the app. A host name, such as chat.example.com, means a different server. The app has no other "active server" variable.

The server that served the app uses a cookie session. Each other server uses bearer tokens that the app gets through a separate sign-in on that server. Only the active server keeps a WebSocket open. For the other servers, the client opens a short connection at intervals to catch up.

The list of servers that you added is the server catalogue. It stays on your device and is not synchronized through an account (ADR-074). ADR-112 makes the catalogue a part of the web app, not of the package, because a bot only needs to connect to a server. That move is not complete: the package still stores the list on the device.

The client does not store chat data on the device (ADR-107). Messages, member lists, profiles, and notification state exist only in memory. Device storage holds only the server catalogue, the authentication records, and UI preferences, such as pane widths.

A copy on disk would keep private data on the device, and each privacy boundary would need a deletion on disk. The cost of this decision is clear. After a reload, the app shows loading states until the server sends a new snapshot.

Read the code

The records behind this chapter

Esc