Skip to content
D
Documentation

Synchronize diagrams

how-to
3 min readUpdated

Connect each diagram instance to a transport and give each peer its own actor id. Connected peers share edits; after a transport reconnects, the session exchanges missed operations so the replicas converge.

Connect two peers

Use the component's collab prop when both diagrams run in a supported framework. Each peer gets its own transport connection and actor id, while both transports connect through the same room. The framework examples use MemoryHub to put two visible peers on one page; edits cross between their canvases. The plain JavaScript render entry point mounts a diagram, but does not expose that component prop.

For the framework bindings and typed graph data, see Execute and compute flows; this page adds the per-peer collab configuration that connects mounted diagrams to the same sync room.

html
<!-- The plain JavaScript render() API mounts a diagram but does not expose a collab option. -->
<div id="app" style="height: 460px"></div>
<script type="module">
  import { render } from '@grafloria/element';

  const container = document.getElementById('app');
  const instance = render({
    nodes: [
      { id: 'a', label: 'Alpha', position: { x: 80, y: 90 }, size: { width: 150, height: 66 } },
      { id: 'b', label: 'Beta', position: { x: 320, y: 90 }, size: { width: 150, height: 66 } },
    ],
    edges: [{ id: 'e1', source: 'a', target: 'b' }],
  }, container);

  instance.fitView();
</script>

Each Angular, Qwik, React, and Vue version mounts two canvases with the same two nodes and connecting edge. Move a node in either pane: the other pane receives the operation and renders the edit. The framework canvas joins a CRDT sync session when it mounts and leaves when it unmounts; set up its collaboration options before mounting.

The diagram data is the document peers converge on. CRDT operations merge per property, so concurrent changes to different properties of the same node—for example, one peer moving it while another renames it—both survive. This is not whole-node last-write-wins replacement.

Options and boundaries

OptionTypeDefaultWhat it does
collabObject containing a transport and actor idUnsetJoins the canvas to a sync session. Give each peer a distinct actor id and connect its transport to the same room.

The collaboration option is fixed for the lifetime of a mounted instance. If a peer needs a new transport or actor, mount an instance with that configuration rather than changing collab in place. In Qwik, create transports in useVisibleTask$ and retain them with Qwik's non-serializable state handling; a live transport is not serializable application data.

Grafloria starts an anti-entropy round when a transport reports that it has reconnected, exchanging the operations each peer missed. Grafloria supplies convergence and the operation log, not your product's rooms, authentication, or storage; implement those at the transport and application layers.

See the synchronization states

In the conflict-resolution demo, peer A moves a node while peer B renames it. The status readouts disagree before exchange and show both edits after they converge. The initial view shows both peers with the same Draft node.

Open the live conflict-resolution demo.

For disconnected edits, open the offline-and-reconnect demo. Disconnect both peers, edit each side, and reconnect; the missed operations are exchanged and both diagrams converge. Its initial view shows two matching diagrams with Alpha connected to Beta.

Open the live offline-and-reconnect demo.

For actual cross-tab collaboration without a server, open the two-tabs demo in two browser tabs. It uses BroadcastChannel for transport, so edits in either tab reach the other; each tab keeps its own viewport. Its initial view shows the same two-node diagram in both panes.

Open the live two-tabs demo.

Pitfalls

  • Use one hub/room for peers that share a document, but a distinct actor id for each peer. The sender does not receive its own echo from MemoryHub.
  • A shared transport has to report status changes if you rely on automatic reconnect catch-up.
  • The plain JavaScript render API mounts a diagram but does not accept the framework collab prop. Its sample above demonstrates mounting only; it does not connect peers. Use a framework binding's collaboration prop for peer synchronization.
  • Do not treat the collaboration channel as persistence or authorization. The app still owns rooms, identity, and storage.

Was this page helpful?

Synchronize diagrams — Grafloria · GPT-6 Luna