Collaboration
Folio collaborates the way ProseMirror does: a central authority orders steps. Clients send steps tagged with the version they're based on; the authority accepts only steps based on its current version; every client pulls the steps since its own version and rebases its unconfirmed ones on top (prosemirror-collab). Positions, carets and selection highlights are computed from Folio's layout, so remote cursors land exactly on the paginated text.
One engine, every document type
Documents, slide decks and canvas boards collaborate through the same engine: one protocol, one client, one server, one presence and roles model, one storage and history. What differs per document type is only a small binding that turns that type's model into a Folio (ProseMirror) document and back:
| Layer | Package | What it does |
|---|---|---|
| Engine | @nextgensoftwares/folio-plugin-collab/core | Step sync with rebase, WebSocket transport, reconnects and backoff, presence (extra carries per-type data), offline outbox, roles. No editor UI, no React: for bindings, bots, servers and workers. |
| Engine UI | @nextgensoftwares/folio-plugin-collab | The core plus the document editor plugin and React UI (collaborators bar, version history, diff, blame). |
| Storage | @nextgensoftwares/folio-sync | The server-side document store (chunks, checksums, step log) and the REST client. |
| Server | @nextgensoftwares/folio-server-node | The reference server: REST, /docs/:id/collab rooms, comments, assets. One server built with every binding's schema stores all three types. |
| Binding: documents | @nextgensoftwares/folio-editor | The document is already a Folio document: no conversion. |
| Binding: decks | @nextgensoftwares/folio-slides-collab | Deck ⇄ document (deckDocSchema), presence for slide, selection and pointer. Guide |
| Binding: boards | @nextgensoftwares/folio-canvas-collab | Board ⇄ document (canvasDocSchema), presence for pointers, strokes and laser trails being drawn. Guide |
So a fix to reconnects, a new role, an outbox improvement or a server feature lands once and every document type gets it. Writing a binding shows what a new document type needs.
Layers of the engine
There are three layers, from smallest to most complete.
1. In-memory: LocalAuthority + attachCollab
Built into @nextgensoftwares/folio-editor. Two editors in one page share an authority, as two users would through a server. Steps cross the "wire" as JSON and are rebuilt with the authority's schema, exactly as they would be over a network.
import { attachCollab, collabPlugin, FolioEditor, LocalAuthority } from '@nextgensoftwares/folio-editor';
const writers = [
{ clientID: 'A', name: 'Writer A', color: '#e11d48' },
{ clientID: 'B', name: 'Writer B', color: '#2563eb' },
];
const editors = writers.map((w) => new FolioEditor({ schema, doc, layout, measurer, plugins: [collabPlugin(0, w.clientID)] }));
const authority = new LocalAuthority(editors[0]!.state.doc);
const detach = editors.map((e, i) => attachCollab(e, authority, writers[i]!));
// Presence: everyone's selection, for drawing remote carets.
const others = authority.presence().filter((p) => p.clientID !== 'A');
// <FolioView editor={editors[0]} remotes={others} />attachCollab sends local steps, applies remote ones (mapping the selection backward so your caret stays put), and publishes the selection as presence once in sync. A server implements the same CollabAuthority interface (receiveSteps, stepsSince, setPresence, presence, subscribe) and persists the document.
editor.presenceGeometry(people, window) turns collaborators' { anchor, head } into carets and highlight rects (positions are clamped to this document), and FolioView's remotes prop draws them with name labels.
2. Over HTTP: SyncClient
@nextgensoftwares/folio-sync's SyncClient uses the same step protocol over REST with polling: debounced batched pushes, pull + rebase on conflict, offline outbox, server-side checksums and the mass-delete guard. Two clients editing the same document through it converge; this is covered by packages/sync/src/sync.test.ts. It's the right choice for "Google Docs-style autosave" with occasional concurrent editors. See Loading & saving.
3. Realtime: @nextgensoftwares/folio-plugin-collab in progress
Realtime collaboration over WebSockets: a transport with reconnects and backoff, presence (carets, typing, idle, follow), conflict-safe step sync on @nextgensoftwares/folio-sync's server logic, version history, blame, roles, and an offline flush. The reference server (@nextgensoftwares/folio-server-node) hosts it at /docs/:id/collab: everyone who opens the same document on the same server joins its room. While a document is open live, the socket carries the saves and the REST SyncClient is not used. See the collaboration plugin page and Real networks.
Rules that keep collaboration safe
- Loading is not an edit. Content appended while a document streams in is marked as already confirmed for the collab plugin, so it is never sent back.
- Out-of-band changes are steps. If the server changes a document (replace a chapter, run a migration), it must do so as steps every client rebases on, never as a raw database overwrite: use
documents.edit(orPOST /docs/:id/edit), see Server-side edits. Storage, ownership for several instances, and history sizing are on the Collaboration server page. - Declare every attribute. ProseMirror drops undeclared attributes when steps are rebuilt from JSON; plugins must declare what they store.
- Keep transient state out of the document. Upload progress, selections and hover state are presence or local stores, never steps.
Writing a binding
A binding makes one document type collaborative on the engine. Decks (slides-collab) and boards (canvas-collab) follow the same recipe:
- A schema part. Nodes for the type's containers and items, extending the host schema (
base.extend(...), plus comment anchors), so one server stores every type. Make each property users edit on its own its own attribute: attribute steps are the unit of last-writer-wins, so two people changing different properties of one item both win. Put the rest in one JSON attribute (more). Give every attribute avalidatewith a size limit: steps come from other clients. - Order by keys, not positions. Items carry a fractional order key (
order,z) and are sorted by it; a move is an attribute change, so two people moving the same item can never duplicate it. - Model → document. Turn each local change into steps: from the model's own field-level events (boards:
eventTransaction) or by diffing the model against the document (decks:deckTransaction, skipping unchanged parts by identity). - Document → model. On every editor change that didn't come from the binding (peers' steps, rebases), diff the previous document against the new one by node identity and apply the result to the model as remote changes. Read nodes through a per-node cache, so unchanged items keep their identity and renderers stay cheap.
- Remove duplicates on confirmed documents. Two people restoring the same deleted item each put it back; once nothing is unconfirmed, keep the first copy of each id (every client removes the same copies).
- Presence for the transient. Pointers, selections and anything drawn but not committed ride presence
extra(a JSON object, at mostPRESENCE_EXTRA_CHARS), throttled; validate what peers send before using it, and keep each peer's data under its own namespace. - Undo per user. Either
prosemirror-historyon the shared document (decks) or the model's own per-user history whose undo is a fresh local edit (boards); both only ever undo this user's changes.
Test with the engine's in-memory hub (LocalHub on a VirtualClock, lossy links included) and once against folio-server-node over WebSockets: both bindings' tests show the setup.