API Reference - @liveblocks/react-flow
@liveblocks/react-flow provides you with React hooks and
components that add collaboration to any React Flow
diagram. It adds multiplayer data syncing, document persistence on the cloud,
and realtime cursors.
Read our get started guide to learn more.
Setup
If you’re not already using React Flow, follow their guide to get started. Install it and include its base styles.
Install Liveblocks’ packages:
Import and use the useLiveblocksFlow hook to make React
Flow collaborative:
Then, import and add the Cursors component (alongside Liveblocks’
styles) to add realtime cursors inside React Flow’s canvas:
useLiveblocksFlow
This hook returns a controlled React Flow state made collaborative using Liveblocks Storage.
You can pass initial nodes and edges to the hook which will be set when entering the room for the first time.
- nodes.initialNode[]
Default nodes used when the room has no data yet.
- nodes.syncNodeSyncConfig
Per-type sync configuration for node
datakeys. See Sync config. - edges.initialEdge[]
Default edges used when the room has no data yet.
- edges.syncEdgeSyncConfig
Per-type sync configuration for edge
datakeys. See Sync config. - storageKeystring
The key used to store the flow in Liveblocks Storage. Defaults to
"flow". SeestorageKey. - suspenseboolean
When
true, suspends until the diagram is ready. Learn more about this in the Suspense section.
The options passed to the hook (initial nodes, edges, storage key, Suspense, etc.) are read once when the hook mounts. Later changes to those options will not take effect.
- nodesNode[] | null
Current nodes,
nullwhile loading unless using Suspense, in which case it is always an array. - edgesEdge[] | null
Current edges,
nullwhile loading unless using Suspense, in which case it is always an array. - isLoadingboolean
Whether the diagram is still loading. When using Suspense, always
falseafter the hook has resumed. - onNodesChangeOnNodesChange
Pass to React Flow’s
onNodesChange. - onEdgesChangeOnEdgesChange
Pass to React Flow’s
onEdgesChange. - onConnectOnConnect
Pass to React Flow’s
onConnect. Handles new edges. - onDeleteOnDelete
Pass to React Flow’s
onDelete. Handles node and edge deletions atomically so that deleting a node and its related edges count as a single undoable action.
Local state vs Storage
Some React Flow fields are intentionally not written to Liveblocks Storage so each client keeps their own selection and interaction state:
- Nodes:
selected,dragging,measured,resizing - Edges:
selected
Everything else on nodes and edges (including position, width and height,
data, handles, and edge endpoints) is synchronized through Storage. If you
want specific keys inside node.data or edge.data to stay local-only too, use
the sync config.
Undo / Redo
Undo and redo are automatically enabled for the entire flow state. All synced changes to nodes and edges are recorded on the undo stack, including position changes, data updates, additions, and removals.
A few things are handled automatically:
- Dragging and resizing produce many live updates during a drag, but produce only a single action on the undo stack
- Deleting nodes and edges in a single action will undo together
- Local-only properties are not recorded on the undo stack
To wire up undo/redo in your UI, just use Liveblocks’ normal
useHistory hook:
Custom nodes
Custom nodes work like in any React Flow setup.
Suspense
By default, useLiveblocksFlow returns isLoading: true, nodes: null, and
edges: null while loading. You can use the suspense option to suspend until
the diagram is ready, when doing so, nodes and edges will always be arrays
and isLoading will always be false.
Storage key
By default, useLiveblocksFlow stores nodes and edges under key "flow" in
Liveblocks Storage. Use the storageKey option to choose a different key or to
support multiple diagrams in a single room.
Sync config for node.data
By default, every key inside a node or edge’s data object is getting deeply
synced. Internally objects are stored as LiveObjects, arrays as LiveLists, etc,
to enable fine-grained conflict-free merging automatically. If two users update
different properties on the same node or edge, their changes will get merged
without conflicts.
For some data, this default behavior is not desirable.
Each key in the config accepts a sync mode:
| Mode | Behavior |
|---|---|
true | Deeply sync and allow conflict-free merging (default). |
false | Keep value local-only. Not synced to other clients at all. Other clients will see undefined. |
"atomic" | Synced, but replaced as a whole (last-writer-wins). No automatic conflict resolution. |
{ ... } | Nested config. Applies recursively to sub-keys of the value. |
Use "*" as a fallback for all node (or edge) types.
Cursors
Add the Cursors component inside your ReactFlow component to add realtime
cursors inside React Flow’s canvas. Also import Liveblocks’ styles when using
it.
It works similarly to @liveblocks/react-ui’s
Cursors component.
By default, cursor coordinates are stored in Presence under "cursor". Use
presenceKey to support multiple diagrams in a single room.
User information
Cursors uses
resolveUsers
to resolve each user’s information and then uses the name and color
properties.
Customize cursors
Pass a Cursor component through the components prop to control how each
cursor is rendered. It receives userId and connectionId via its props. Its
position and visibility are still handled by Cursors.
Props
- presenceKeystringDefault is "cursor"
The key used to store cursor coordinates in users’ Presence.
- componentsPartial<CursorsComponents>
Override the component’s components.
Updating your Flow from the server
To update a React Flow from your Node.js back-end, use mutateFlow.
mutateFlow
mutateFlow opens a room’s flow for reading and mutating. A "flow" is a
collection of nodes and edges stored in Liveblocks Storage. By default, your
flow gets persisted in the Room’s Storage under the top-level key "flow", but
you can change this with the storageKey option.
Even though internally mutateFlow uses Liveblocks Storage and its primitives
like LiveObjects, LiveLists, and LiveMaps, you don’t need to work with those
directly. You just work with React Flow nodes and edges as you normally would,
and they get intelligently stored and synchronized via Live structures in the
most efficient manner.
- clientLiveblocksRequired
A Liveblocks Node client instance.
- roomIdstringRequired
The ID of the room whose flow to mutate.
- storageKeystring
The key used to store the flow in Liveblocks Storage. Defaults to
"flow". Must match thestorageKeyused on the client. - nodes.syncNodeSyncConfig
Per-type sync configuration for node
datakeys. Must match the sync config used on the client. - edges.syncEdgeSyncConfig
Per-type sync configuration for edge
datakeys. Must match the sync config used on the client.
Custom node and edge types
Pass your custom Node and Edge types as type parameters to get full type
safety.
MutableFlow API
The callback receives a MutableFlow instance. All reads reflect mutations made
earlier in the same callback.
flow.nodes
Returns the current list of nodes as a readonly array.
flow.edges
Returns the current list of edges as a readonly array.
flow.toJSON
Returns a plain object with nodes and edges arrays.
flow.getNode
Returns a single node by ID, or undefined if not found.
flow.getEdge
Returns a single edge by ID, or undefined if not found.
flow.addNode / flow.addNodes
Adds one or more nodes. If a node with the same ID already exists, it is replaced.
flow.updateNode
Updates a node by ID. Accepts a partial object to merge, or an updater function. No-op if the node does not exist.
When using an updater function, always return a new object. Never mutate the provided value in-place.
flow.updateNodeData
Updates only a node’s data by ID. Accepts a partial object to merge, or an
updater function. No-op if the node does not exist.
When using an updater function, always return a new object. Never mutate the provided value in-place.
flow.removeNode / flow.removeNodes
Removes one or more nodes by ID.
flow.addEdge / flow.addEdges
Adds one or more edges. If an edge with the same ID already exists, it is replaced.
flow.updateEdge
Updates an edge by ID. Accepts a partial object to merge, or an updater function. No-op if the edge does not exist.
When using an updater function, always return a new object. Never mutate the provided value in-place.
flow.updateEdgeData
Updates only an edge’s data by ID. Accepts a partial object to merge, or an
updater function. No-op if the edge does not exist.
When using an updater function, always return a new object. Never mutate the provided value in-place.
flow.removeEdge / flow.removeEdges
Removes one or more edges by ID.
How mutations are flushed
The mutateFlow function is built on top of Liveblocks.mutateStorage,
but rather than giving you access to the Liveblocks Storage root directly, it
gives you a convenient API tailored to React Flow—a MutableFlow instance you
can use to read or mutate your nodes and edges directly.
Under the hood, everything still gets stored and synced efficiently via Live
structures (LiveObject, LiveMap, etc), but this is an implementation detail.
Just like with Liveblocks.mutateStorage, your mutation callback can be
long-running. During its execution, any mutations you make will periodically get
flushed to the server in the background, and when your callback finishes, the
final state will be flushed before the promise returned by mutateFlow
resolves.