# A shared whiteboard

> A whiteboard for phones and computers with no server of your own: Yjs keeps the board, y-jsflow syncs it over a room, y-indexeddb keeps it in each browser, and edits made offline merge on return.

A whiteboard is an open canvas where several people draw, place sticky notes and shapes, move them, and see each
other's cursors live. This page walks through the [whiteboard example](https://github.com/precisit/jsflow-community/tree/main/examples/whiteboard):
a complete board for phones and computers, with **no server of your own**. Someone opens a board, shows its QR
code, and the others scan it and draw. **[Try it live](https://jsflow.com/examples/whiteboard/)**: open it in two tabs, or on a phone and a computer.

The example puts each job where it belongs:

| | where | why |
|---|---|---|
| the board (lines, notes, shapes) | a Yjs document, in every browser | any two copies merge, so edits made at the same moment or offline never conflict |
| changes to the board | the jsflow room, through [y-jsflow](https://jsflow.com/docs/guides/yjs.md) | small changes as datagrams, a joiner's whole board as one reliable stream |
| cursors, names, the laser pointer | Yjs awareness, over the room | they matter only now: sent often, never stored |
| the board between visits | IndexedDB in each browser ([y-indexeddb](https://github.com/yjs/y-indexeddb)) | jsflow keeps no room state |

## Run it

```sh
git clone https://github.com/precisit/jsflow-community
cd jsflow-community/examples/whiteboard
npm install
```

Put your app's publishable key in `main.js` (`APP`), with `http://localhost:5173` among the app's origins, then
`npm run dev` and open the address it prints in two windows. **Share** shows the board's QR code and link.

## The board as Yjs data

The whole board is one `Y.Map` of shapes, keyed by a random id. Each shape is its own `Y.Map`:

```js
const shapes = doc.getMap('shapes');
const note = new Y.Map();
note.set('type', 'note');
note.set('x', 120); note.set('y', 80);
note.set('w', 180); note.set('h', 140);
note.set('text', new Y.Text('Plan for Monday'));
shapes.set(id, note);
```

- **Two people change one shape:** different fields (one moves it, one recolours it) both stay; the same field
  (both drag it) ends with one value on every screen. There is no host to ask and no lock.
- **A line being drawn** is a `Y.Array` of points that grows while the pen moves. Each batch of points, every
  33 ms, is a small Yjs update that y-jsflow broadcasts as one datagram, so the others see the line grow.
- **A note's text** is a `Y.Text`: two people typing in one note both keep their words.
- **Undo** is `Y.UndoManager`, tracking only this page's own changes: Ctrl+Z never takes back someone else's line.

## Joining the room

```js
// one Awareness for the whole visit, through every reconnect
const awareness = new Awareness(doc);
const room = await join({ app: APP, room: name });
const local = new IndexeddbPersistence(`whiteboard-${name}`, doc);
// the copy in this browser: the board shows at once
await local.whenSynced;
const provider = new JsflowProvider(room, doc, {
  awareness,
  leaveHoldMs: 2000,
});
```

The board's name is the room's name, and it is in the address (`#…`), so the QR code is just the page's address.
Anyone with it can join, so a new board's name is 16 random characters. To decide who may join, give the app
[join tickets](https://jsflow.com/docs/guides/join-tickets.md).

A joiner asks one peer for the whole board, preferring a direct link. The people list shows how this page reaches
each person, from `room.path(peer)`: direct, via the relay, or via the others.

## Cursors and the laser pointer

Each page puts its name, colour, cursor, selection and laser state in `provider.awareness`. y-jsflow sends every
awareness change, and a pointer reports up to 240 moves a second, so the example sends **at most 25 updates a
second** (`rate.js`) and smooths the cursor at the receiver. The laser pointer is a flag in the same state: the
receivers draw a trail that fades in a second, and nothing is stored.

## Offline, and coming back

The status button switches the board **offline**: the page leaves the room (`provider.destroy()`,
`room.leave()`) and keeps drawing on its own copy. Back **online**, it joins again with a new provider, and the
two sides exchange state vectors: each sends exactly what the other lacks. The new provider gets the same
`Awareness`, so its clock keeps counting and the others take the returning cursor as new. A new `Awareness` for
the same document would start its clock again, and the others would ignore it until it passed the old one. The page counts the shapes that went
each way:

> Back online: sent 2 shapes, got 1 shape.

A phone page that was hidden comes back the same way, and so does a laptop that lost its network.

## What is kept, and where

- **A reload** shows the board at once, from IndexedDB, before the room has synced.
- **When everyone leaves**, the board is still in each of their browsers. Whoever opens the link next has their
  copy, and the copies merge when others join.
- **Download board file** saves the whole board (`Y.encodeStateAsUpdate`); **Open a board file** opens it as a
  new board. **Download as PNG** saves a picture.
- A browser that clears its site data loses its copy. To keep boards beyond the browsers, save the same update
  on your backend and load it before the provider starts ([Keeping the document](https://jsflow.com/docs/guides/yjs.md#keeping-the-document)).

## Measured


Measured on 2026-11-23 against production. Every peer was its own headless Chromium, all on one lab machine, and
"relay only" went through production's relay. So the times include no distance between people: between two
homes, add the network's own delay.

| | direct | relay only |
|---|---|---|
| a board of 1,000 freehand lines | 0.94 MB as Yjs encodes it | |
| a late joiner, from opening the page to that board shown (median of 5, two runs) | 3.3 to 3.6 s | 5.0 to 5.2 s |
| of which the board itself, first byte to last | 0.65 s | 2.0 s |
| a line's new points, from the drawer's board to the receiver's (median / 95th percentile, 300 lines) | 3 / 8 ms | 7 / 11 ms |
| 16 people moving their cursors at once | each page receives 369 updates a second (of 375 sent to it), 48 KB/s; a cursor moves every 41 ms (95th percentile 44 ms) | |

Most of a join is the page starting, signalling and the first link; the board's 0.65 s includes about 0.3 s of
drawing it. A line's points also wait up to 33 ms on the drawer's side, where they are batched.

## Limits

- Peers per room follow your plan: 20 on Free, 50 on Pro and up.
- Each person on a board counts toward your [peer-minutes](https://jsflow.com/pricing/): one peer in a room for a minute, while
  the room has two or more peers and the peer is not away. Someone alone on a board, or a phone whose page is
  hidden, counts nothing.
- Keep a board to a few megabytes: a joiner receives all of it from one peer.
- A closed tab cannot announce its leave over the room, so the others see its cursor for up to about 3 seconds.
- A hidden iPhone page leaves the room until it is shown again; its edits merge when it returns.
- Devices on a board link directly when they can, so each can see the others' IP addresses. The example says so
  where you share a board; see [security and privacy](https://jsflow.com/docs/guides/security-and-privacy.md#peer-ip-addresses).
- The page needs the network to open (there is no service worker). Once open, it keeps working without one, and
  its edits merge when the network is back.

## Next

- [Collaborative documents with Yjs](https://jsflow.com/docs/guides/yjs.md): the provider's options and how it syncs.
- [Presence](https://jsflow.com/docs/guides/presence.md): who is here, without Yjs.
- [Limits](https://jsflow.com/docs/guides/limits.md): message sizes and rates.

Source: https://jsflow.com/docs/guides/whiteboard/
