Learn React Flow and how to sync in real-time between participants
Posted by Vitor Norton | April 23, 2024
Today I want to present to you this powerful tool for creating a node-based application: the React Flow. It is a library for building anything from simple static diagrams to data visualizations to complex visual editors. In this article I will show you how to create a simple React Flow application, understand more about nodes and edges, and add real-time synchronization between participants in the same content.
Installation
To begin with React Flow, you first will need to have a React base application and add the package reactflow
to it, you can use any package manager
1npm install reactflow
Nodes and Edges
Before starting to use it, you should be familiarized with what a node and an edge is.
In simple terms, nodes are the primary elements or objects that you want to represent in your graph, and edges are the connections or relationships between these nodes. For example, in a social network, each person could be a node, and the friendships between them could be the edges.
Technically speaking, React Flow has these two types: the Node, which will contain an ID, a position and its data, and the Edge which will contain an ID for it, a source and target. In the code below I created a list of initial nodes (a couple of blocks) and a list of initial edges: connecting the first node to the second node.
1export const initialNodes = [2{3id: '1',4position: { x: 200, y: 200 },5data: { label: 'First block' },6},7{8id: '2',9position: { x: 250, y: 300 },10data: { label: 'Second block' }11},12]1314export const initialEdges = [15{16id: 'some-id',17source: '1',18target: '2'19}20]
Start using React Flow
After creating the initial nodes and edges, you are now able to add the ReactFlow
component to your application. Such as shown below:
1import React from 'react';2import ReactFlow from 'reactflow';34import 'reactflow/dist/style.css';56export default function App() {7return (8<div style={{ width: '100vw', height: '100vh' }}>9<ReactFlow nodes={initialNodes} edges={initialEdges} />10</div>11);12}
In the code above we create a full screen container for our nodes. We also import the styles from reactflow/dist/style.css
.
To make manipulation of the data easy, I’m going to use the useNodesState
and useEdgesState
hooks, which work like the useState
hook. With an addiction to a callback function when there are changes on the state.
1import ReactFlow, { useNodesState, useEdgesState } from 'reactflow'23export default function App() {4const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes)5const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges)67return (8<div style={{ width: '100vw', height: '100vh' }}>9<ReactFlow nodes={nodes} edges={edges} />10</div>11);12}
To create a better experience for our users, let’s add some controls, allowing them to zoom in and out, to see a mini map of their content and other features. We will add it as a child to our ReactFlow
component.
1<ReactFlow nodes={nodes} edges={edges}>2<Controls />3<MiniMap />4</ReactFlow>
To make a great background, you can also add the Background element, in which you will be able to choose variants of background like dots, crosses or lines as well their colors.
1<Background variant={BackgroundVariant.Cross} color='#f00' />
Synchronizing between users
If you want to create a collaborative environment where multiple users can interact with the same React Flow application at the same time, you will need to use a SuperViz Real-time. This engine allows any changes made by one user to be instantly visible to all other users in the same room. This involves creating a unique room for each group of users and synchronizing their activities.
Creating a shared room
A room is a virtual space where users can join and collaborate. When creating a room, you can specify a unique ID for it.
To create a room, you need to use the SuperVizRoom
that’s available from the @superviz/sdk
package, which takes the Developer Key and an object as a parameter. The object should have the following properties:
id
: The ID of the room, which should be a unique string shared between the participants of that room.participant
: An object that contains information about the current user, such asname
,id
.group
: An object that contains information about the group that the user belongs to, such asname
andid
.
Here’s an example of how to create a room with SuperViz:
1// Import the SuperViz SDK2import SuperVizRoom from '@superviz/sdk';34// Create a room object5const room = await SuperVizRoom(DEVELOPER_KEY, {6roomId: "ROOM-ID",7participant: {8id: "USER-ID",9name: "USER-NAME"10},11});
Adding SuperViz Real-time
To add the event broker, which will be responsible for notifying when a change is made and to listen to state changes. Right after creating a room, you can add the engine to it.
1const realtime = new Realtime();2room.addComponent(realtime);
Listening to state changes
To make full synchronization we are going to listen to a couple of events: new-edge
and node-drag
, which happens when we connect one React Flow node to another and when we drag a node across the canvas.
Let’s first create a function to handle when new edges are added, and then subscribe it to the new-edge
event.
1function onNewEdgesAdded({data, participantId}){2// Verify if the origin of the event dispatch isn't the same participant on the page3// preventing a infinity loop4if (participantId === currentParticipantId) return;56setEdges((eds) => addEdge(data.edge, eds));7}89realtime.subscribe('new-edge', onNewEdgesAdded)
Now, we will do something similar to listening to node-drag
. In this code we will update our nodes with the data, containing its new position, that we receive from the event.
1function onNewEdgesAdded({data, participantId}){2// Verify if the origin of the event dispatch isn't the same participant on the page3// preventing a infinity loop4if (participantId === currentParticipantId) return;56setEdges((eds) => addEdge(data.edge, eds));7}89realtime.subscribe('new-edge', onNewEdgesAdded)
Publishing an event
We are now listening to the events, but to trigger these events, we need to publish them. We can do this by using the publish
method from the realtime
object. Whenever a user adds a new edge or drags a node, we will call realtime.publish
, passing the name of the event and the relevant data. This will then trigger the corresponding event listeners in other users’ applications, keeping all participants in sync.
To understand when new changes are made on both scenarios, we need to use React Flow’s callback functions onConnect
and onNodeDrag
, such as the code below:
1<ReactFlow2...3onNodeDrag={onNodeDrag}4onConnect={onConnect}>5</ReactFlow>
The implementation of the onNodeDrag
is straightforward, as shown below:
1function onNodeDrag(event, node) {2realtime.publish('node-drag', { node });3}
To publish the new-edge
event on a new connection (onConnect
) it’s a bit different, we first need to recreate the edge object, add it to our current list using the React Flow’s addEdge
method, and then publish to SuperViz Real-time.
1function onConnect(params) => {2const edge = {3...params,4type: ConnectionLineType.SmoothStep,5animated: true,6}78setEdges((edges) => addEdge(edge, edges))910realtime.publish('new-edge', { edge })11}
Conclusion
In this tutorial, we’ve learned the basics of React Flow and how to create a real-time, collaborative environment using SuperViz’s SuperViz Real-time. You can see this project in functioning on our demo page dedicated to React Flow, as well the source code on our GitHub repository.
Happy coding!
Recent posts
This website uses cookies to give you the best experience. See our Privacy Policy for further details.