Skip to content

Collaboration

Two independent canvases with two independent documents, synchronized by exchanging operations. Draw on either side and the stroke appears on the other, with the other person's cursor and their stroke in progress shown live. Add latency, take a participant offline, or reload one to watch it join again. This runs entirely in the browser over an in-memory transport; a real application implements the same interface over SignalR or WebSockets.

Alex
Empty drawing canvas.
0 elements sent 0 messages, 0 Bconnected
Sam
Empty drawing canvas.
0 elements sent 0 messages, 0 Bconnected

What travels on the wire

Drawing a stroke sends that stroke once, as a single operation, in the same compact binary point encoding as the file format. Nothing else is transmitted: not the other strokes, not the document. Undo and redo are sent too, because to everyone else they are ordinary edits. While the pen is down, the stroke in progress travels as presence, throttled, so the other side sees it being drawn. The most recent operation is shown below exactly as it was sent; every incoming one is validated like a loaded file, so a peer cannot send something hostile.

Draw on one of the canvases to see the operation that is sent.

A picture crosses the wire once: later changes to it, such as every frame of a drag, refer to it by a hash of its content instead of sending it again.

Conflicts and going offline

Conflicts are resolved by last-writer-wins on each element's version, with the participant id breaking ties so every replica reaches the same result. Two people drawing at once never conflict, because each stroke is a new element with its own id. Take a participant offline and draw on both sides: the offline side queues what it sends (PendingCount, shown above; IsSynchronized is true once it is empty), the server keeps what is sent to it, and on reconnect both catch up until the replicas are identical. Presence is not queued, because a cursor position from a minute ago is noise, not news. BinkCollaborationOptions sets how much is queued (MaxOfflineQueue) and the limits incoming operations are held to (Limits), the same BinkDocumentLimits a loaded file is.

Two limits are worth knowing. Two people dragging the same element at the same moment: one drag wins, and every replica agrees which. And stacking order is not reconciled: concurrent "bring to front" and "send to back" can leave the same elements stacked differently on each side. Nothing is lost, and SendSnapshotAsync brings everyone back to one order.

Joining, and starting over

Reload does what reloading the page does: the session leaves, which drops its cursor from the other side at once, and a new one joins with an empty document and calls RequestSnapshotAsync. Every participant that serves snapshots (ServeSnapshots, on by default) answers with the whole document, addressed to the one that asked; the first answer is applied as one remote change, so it never lands in the newcomer's undo history, and anything drawn while it was on its way is applied again on top.

var session = new BinkCollaborationSession(new BinkDocument(), transport, participantId: "sam");
await session.RequestSnapshotAsync();   // joining: someone sends this participant the document

await session.SendSnapshotAsync();      // resetting: everyone replaces their copy with this one

SendSnapshotAsync is the other direction, for putting everyone back on one version, such as after restoring a saved document. When one transport carries several documents, IgnoreOtherDocuments makes a session drop the traffic of the others.

Wiring a session

Both participants start from the same document, layer ids included - which is what joining a session does in a real application: load a snapshot. A session then joins that document to a transport. The transport here is the demo's in-memory LoopbackTransport (Shared/LoopbackTransport.cs); anything that implements IBinkCollaborationTransport - a SignalR hub, a WebSocket - takes its place unchanged.

_documents[1] = BinkSerializer.Deserialize(BinkSerializer.Serialize(_documents[0]));

LoopbackTransport transport = _hub.CreateClient();
var session = new BinkCollaborationSession(_documents[i], transport, Participants[i].Id);

// Cursors and the stroke in progress travel as presence, not as operations.
await session.UpdatePresenceAsync(new BinkPresenceState { ParticipantId = "alex", Cursor = position });
session.PresenceChanged += presence => { /* redraw their cursor */ };
session.ParticipantLeft += id => { /* remove it */ };

Presence is throttled to PresenceInterval (50 ms) and expires after PresenceTimeout; this page calls FlushPresenceAsync on a timer so the last position of a fast movement is not held back by the throttle. The cursors are drawn by the page itself (Shared/PresenceOverlay.razor), as plain SVG over the canvas, so only that overlay re-renders when they move.

A document belongs to one thread, which in Blazor is the renderer's dispatcher. The loopback transport already runs there; a real one does not - SignalR raises its callbacks on the thread pool - so it has to hand each message to the component's InvokeAsync before raising MessageReceived, or a message could be applied while the local user is drawing. Each sender's messages must arrive in order, which SignalR and WebSockets both give; ordering between senders does not matter.

connection.On<BinkCollaborationMessage>("Message", message =>
    dispatch(() => MessageReceived?.Invoke(message) ?? Task.CompletedTask));   // dispatch = the component's InvokeAsync
An unhandled error has occurred. Reload 🗙