Collaborating on boards
Canvas boards (lecture boards and whiteboards, @nextgensoftwares/folio-canvas) collaborate on Folio's one collaboration engine, the same one documents and decks use. @nextgensoftwares/folio-canvas-collab is the binding: it turns a board into a Folio document and back, and carries what's being drawn over presence. API: folio-canvas-collab.
How it fits
A board is a shared document with one canvasPage block per page, each holding one atom canvasObject per object (canvasDocSchema()):
- Every property is its own attribute (
x,y,w,h,rotation,fill,text,points… seeOBJECT_FIELDS); fields a host adds ride inmore. Board edits are already field-level events with exact inverses, so each maps one to one onto attribute steps. - Pages are ordered by an
orderkey and objects by theirzkey (fractional, plain string order), not by node position: raising an object or moving a page is an attribute change. - Local events become steps (
eventTransaction); peers' steps become remote events on the board's store (docChanges, diffed by node identity, so the cost is what changed). The store always shows the shared document.
The server verifies every push as it does for documents (version, step application against the schema, checksum), and the same roles, comments, storage and history apply.
Merging rules
| Concurrent edits | Result |
|---|---|
| Different fields of one object (one moves a sticky, the other recolours it) | both apply |
| The same field | the later write in server order wins |
| Edit vs delete of the same object | the delete wins; the edit is dropped on rebase |
| Two creates | both objects exist; equal z keys tie-break by id, the same everywhere |
| Two people restore the same deleted object | the first copy stays (duplicates are removed on the confirmed document) |
| Typing in the same sticky at once | the last text wins (text is one field; character-level merging is a later step) |
Undo
Undo stays the board's own per-user history (createHistory(store)): it only undoes this user's edits, and an undo is a fresh local edit, so it syncs like any other and skips steps a collaborator already undid.
Presence
Presence extra.canvas carries this user's page, pointer, and the newest points of strokes and laser trails while they're being drawn, so collaborators see ink appear under the pen, not when it lifts. Each message holds a rolling window of a stroke's latest points with the index of the first; receivers append what they haven't seen. When the stroke is committed its document step replaces the live preview.
What a peer sends is validated (readPresence) and kept under that peer's namespace (peerStreamId), so no one can extend or cancel another's stroke; each remote stroke and trail is capped in points and age (PEER_LIMITS), and the canvas store bounds laser trails itself. Pointers become cursor events (drawn with the collaborator's name and colour); a peer who leaves takes their cursor and in-flight ink with them.
Set it up
The server is the reference server built with the board schema (add other bindings' schemas to the same base to serve decks and documents too):
import { createFolioServer } from '@nextgensoftwares/folio-server-node';
import { toProseMirrorSchema } from '@nextgensoftwares/folio-editor';
import { canvasDocSchema } from '@nextgensoftwares/folio-canvas-collab';
const pm = toProseMirrorSchema(canvasDocSchema());
await createFolioServer({ schema: pm, allowCreate: true }).listen(8732);A client creates or opens a board, binds a store to it and mounts a board on that store:
import { createCanvasDoc, createCollabCanvas, loadCanvasDoc } from '@nextgensoftwares/folio-canvas-collab';
import { WebSocketTransport } from '@nextgensoftwares/folio-plugin-collab/core';
import { createHttpApi } from '@nextgensoftwares/folio-sync';
import { createCanvasBoard } from '@nextgensoftwares/folio-canvas-board';
const api = createHttpApi({ baseUrl: 'https://boards.example.com' });
await createCanvasDoc(api, 'lecture-7', initialBoard, 'Lecture 7'); // once
const { doc, version } = await loadCanvasDoc(api, 'lecture-7');
const live = createCollabCanvas({
doc: pm.nodeFromJSON(doc), version, clientID, docId: 'lecture-7', user,
transport: new WebSocketTransport('wss://boards.example.com/docs/lecture-7/collab'),
});
const board = createCanvasBoard(el, { store: live.store });
el.addEventListener('pointermove', (e) => live.setPointer(pagePointOf(e)));live.client is the engine's CollabClient (status, peers, stop()); live.editor is the ProseMirror side (positions for comment anchors). Recording keeps working: the recorder listens to the store, and local and remote edits both land there.
The canvas board demo has this wired: node demo/server.mjs, then open /canvas-board/demo/?collab=http://127.0.0.1:8732&name=Sara in two windows.