Rebasely Beta: data may be purged

examples/live-notes Public

Markdown · 57 lines · 2.4 KiB · at 99dd857

---id: note-roomtype: conceptowner: justinreviewed: 2026-10-15component: note-roomdescribes:  - src/note-room.ts  - src/room.ts  - src/index.ts---# The note roomEach note is one Durable Object of class `NoteRoom`, found with `idFromName(noteId)` through the`NOTES` binding. Everyone who opens the same note id reaches the same object, wherever theyare, and that object is the single sequencer described in [[conflict-strategy]].## Two layers`NoteRoom` (`src/note-room.ts`) is the part that knows about the runtime: it accepts sockets,reads frames, sends replies and broadcasts, and owns the SQLite storage. Everything it decidesis delegated to `Room` (`src/room.ts`), which takes a `NoteStore` and returns, for eachmessage, a reply for the sender and an optional broadcast for the others. The tests drive`Room` with an in-memory store, so they run in plain Node without workerd.## StorageTwo tables in the object's SQLite database:- `note`: one row with the text and its version. `Room` caps the text at 100,000 UTF-16 code  units, so the row and every snapshot frame stay small.- `ops`: the op log, keyed by the version each op produced, trimmed to the last 200.`commit()` writes the new text, appends the op and trims the log inside one `transactionSync`,so a note and its log never disagree. The room only updates its in-memory copy after thewrite.## HibernationSockets are accepted with `ctx.acceptWebSocket`, the Hibernation API. When nobody has typed fora while the runtime evicts the object but keeps the sockets open; the next frame wakes it,runs the constructor, and calls `webSocketMessage`. The constructor creates the tables ifneeded and loads the note, so waking up is the same as starting fresh. Nothing the room needslives only in memory.Broadcasting uses `ctx.getWebSockets()`, which lists every accepted socket even after a wake,so there is no connection list to rebuild.Each socket also carries its editor (an id, a name and a color) as a serialized attachment,set right after `acceptWebSocket`. Attachments survive hibernation, so after a wake the roomcan still say who sent a frame and who is here without reading storage. [[presence]] coversthe rest.## Reads without a socket`GET /notes/:id` calls the `read()` RPC method and returns `{ id, text, version }`. It reads theroom's in-memory copy, which is always the last committed state.