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

1
npm 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.

1
export const initialNodes = [
2
{
3
id: '1',
4
position: { x: 200, y: 200 },
5
data: { label: 'First block' },
6
},
7
{
8
id: '2',
9
position: { x: 250, y: 300 },
10
data: { label: 'Second block' }
11
},
12
]
13
14
export const initialEdges = [
15
{
16
id: 'some-id',
17
source: '1',
18
target: '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:

1
import React from 'react';
2
import ReactFlow from 'reactflow';
3
4
import 'reactflow/dist/style.css';
5
6
export default function App() {
7
return (
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.

1
import ReactFlow, { useNodesState, useEdgesState } from 'reactflow'
2
3
export default function App() {
4
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes)
5
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges)
6
7
return (
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 as nameid.
  • group: An object that contains information about the group that the user belongs to, such as name and id.

Here’s an example of how to create a room with SuperViz:

1
// Import the SuperViz SDK
2
import SuperVizRoom from '@superviz/sdk';
3
4
// Create a room object
5
const room = await SuperVizRoom(DEVELOPER_KEY, {
6
roomId: "ROOM-ID",
7
participant: {
8
id: "USER-ID",
9
name: "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.

1
const realtime = new Realtime();
2
room.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.

1
function onNewEdgesAdded({data, participantId}){
2
// Verify if the origin of the event dispatch isn't the same participant on the page
3
// preventing a infinity loop
4
if (participantId === currentParticipantId) return;
5
6
setEdges((eds) => addEdge(data.edge, eds));
7
}
8
9
realtime.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.

1
function onNewEdgesAdded({data, participantId}){
2
// Verify if the origin of the event dispatch isn't the same participant on the page
3
// preventing a infinity loop
4
if (participantId === currentParticipantId) return;
5
6
setEdges((eds) => addEdge(data.edge, eds));
7
}
8
9
realtime.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
<ReactFlow
2
...
3
onNodeDrag={onNodeDrag}
4
onConnect={onConnect}>
5
</ReactFlow>

The implementation of the onNodeDrag is straightforward, as shown below:

1
function onNodeDrag(event, node) {
2
realtime.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.

1
function onConnect(params) => {
2
const edge = {
3
...params,
4
type: ConnectionLineType.SmoothStep,
5
animated: true,
6
}
7
8
setEdges((edges) => addEdge(edge, edges))
9
10
realtime.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

Realtime meets AI: How we used gaming to showcase our new library, now open source!

November 11, 2024

Announcing the Winners of Super Hackathon 2.0: Celebrating Innovation and Creativity

November 01, 2024

SuperViz Hackathon: AI meets RealTime

October 02, 2024