Developer guide Internals
Sync server
The y-websocket compatible room server that syncs and saves every canvas.
On this page
The sync server is server/src/yroom.ts. It is a small Yjs room server that speaks the y-websocket protocol, so the standard y-websocket client connects to it without changes.
Connecting
The HTTP server accepts websocket upgrades on /yjs/<canvas id>. The room name must match ^[A-Za-z0-9_-]{1,64}$. Any other upgrade is closed at once. Messages can be up to 16 MiB (maxPayload).
On the client, openRoom connects like this:
const doc = new Y.Doc();
const local = new IndexeddbPersistence(`archipelego:${id}`, doc);
const proto = location.protocol === 'https:' ? 'wss' : 'ws';
const provider = new WebsocketProvider(`${proto}://${location.host}/yjs`, id, doc);The IndexedDB copy keeps every edit in the browser. A dropped connection loses nothing, and the edits sync when the connection comes back.
Messages
Each websocket message starts with a varint message type:
| Type | Name | Handling |
|---|---|---|
0 |
sync | Read with y-protocols/sync. A reply is sent when the sync step needs one. |
1 |
awareness | Applied to the room's Awareness, then broadcast to everyone. |
When a client connects, the server waits for the room to load. Then it sends sync step 1 and the current awareness states.
Rooms
- The server keeps one
Roomper open canvas: aY.Docand anAwareness. - The first connection loads
<data folder>/<canvas id>.yjsinto the document, if the file exists. - Each document update is broadcast to every connection in the room.
- Each connection remembers the awareness client ids it controls. When it closes, those states are removed.
- When the last connection leaves, the room saves and unloads from memory.
Saving
- Saves are debounced by 1 second after the last update.
- The server writes
<canvas id>.yjs.tmpfirst, then renames it over<canvas id>.yjs. A crash never leaves half a file. - A failed save marks the room dirty again, so the next update retries.
- On
SIGINTorSIGTERM,saveAllsaves every open room before the process exits.
Keeping connections alive
The server pings each connection every 30 seconds. A connection that did not answer the previous ping is closed.
Versioning
The server is the only writer of the patch digit, so concurrent editors never collide. After any transaction that changes the document, it waits 1.5 seconds for edits to settle, then sets meta.version with bumpPatch. It skips its own version writes, loads, and transactions that only change meta.version. See Data model.
Listing canvases
listCanvases reads the .yjs files in the data folder, newest first, at most 50. It uses the live document when the room is open. It returns id, title, version, blocks and updatedAt, and leaves out canvases with no blocks. GET /api/canvases returns this list.
Awareness fields
The client publishes these fields in awareness:
| Field | Value |
|---|---|
user |
{ name, critter, color, device }. device is wall, laptop, tablet or phone. |
cursor |
{ x, y } in world coordinates, or null. Sent about 20 times per second. |
selection |
The selected block id, or null. |
typing |
true while the person types in the chat. It resets after 3 seconds. |
present |
The feature, scenario and step this person presents to the room, or null. |
The presence colour comes from the awareness client id: PRESENCE[clientId % 6] in colorFor.