Quickstart - Get started with React Flow using Liveblocks and Next.js

Liveblocks is a realtime collaboration infrastructure for building performant collaborative experiences. Follow the following steps to start adding collaboration to your Next.js application using the APIs from the @liveblocks/react-flow package.

Quickstart

  1. Install Liveblocks and React Flow

Every Liveblocks package should use the same version.

npm install @liveblocks/client @liveblocks/react @liveblocks/react-ui @liveblocks/react-flow @xyflow/react
  1. Initialize the liveblocks.config.ts file

We can use this file later to define types for our application.

   npx create-liveblocks-app@latest --init --framework react
  1. Create a Liveblocks room

Liveblocks uses the concept of rooms, separate virtual spaces where people collaborate, and to create a realtime experience, multiple users must be connected to the same room. When using Next.js’ /app router, we recommend creating your room in a Room.tsx file in the same directory as your current route.

Set up a Liveblocks client with LiveblocksProvider, join a room with RoomProvider, and use ClientSideSuspense to add a loading spinner to your app.

   "use client";
   import { ReactNode } from "react";
   import { LiveblocksProvider, RoomProvider, ClientSideSuspense } from "@liveblocks/react/suspense";

export function Room({ children }: { children: ReactNode }) {  
       return (    
           <LiveblocksProvider publicApiKey={"pk_prod_xxxxxxxxxx…xxxxxxxxpk_prod_xxxxxxxxxxxxxxxxxxxxxxxx"}>
               <RoomProvider id="my-room">
                   <ClientSideSuspense fallback={<div>Loading…</div>}>
                       {children}
                   </ClientSideSuspense>
               </RoomProvider>
           </LiveblocksProvider>  
       );
   }
  1. Add the Liveblocks room to your page

After creating your room file, it’s time to join it. Import your room into your page.tsx file, and place your collaborative app components inside it.

   import { Room } from "./Room";
   import { Flow } from "./Flow";

export default function Page() {  
       return (    
           <Room>      
               <Flow />  
           </Room>  
       );
   }
  1. Set up the collaborative React Flow diagram

Now that Liveblocks is set up, integrate React Flow in the Flow.tsx file. useLiveblocksFlow keeps nodes and edges in sync across clients using Liveblocks Storage.

   "use client";
   import { ReactFlow } from "@xyflow/react";
   import { useLiveblocksFlow } from "@liveblocks/react-flow";
   import "@xyflow/react/dist/style.css";
   
   export function Flow() {  
       const { nodes, edges, onNodesChange, onEdgesChange, onConnect, onDelete } = useLiveblocksFlow({
           suspense: true,
           nodes: {  
               initial: [
                   { id: "1", type: "input", data: { label: "Start" }, position: { x: 250, y: 0 } },
                   { id: "2", data: { label: "Process" }, position: { x: 100, y: 110 } },
                   { id: "3", type: "output", data: { label: "End" }, position: { x: 250, y: 220 } },
               ],
           },
           edges: {  
               initial: [
                   { id: "e1-2", source: "1", target: "2" },
                   { id: "e2-3", source: "2", target: "3" },
               ],
           },
       });
    
       return (    
           <div style={{ width: "100%", height: "100vh" }}>      
               <ReactFlow        
                   nodes={nodes}        
                   edges={edges}        
                   onNodesChange={onNodesChange}        
                   onEdgesChange={onEdgesChange}        
                   onConnect={onConnect}        
                   onDelete={onDelete}        
               />    
           </div>  
       );
   }
  1. Add multiplayer cursors

To show where collaborators are pointing in the diagram, render Cursors inside ReactFlow. Also include the Liveblocks styles in addition to the React Flow ones.

   "use client";
   import { ReactFlow } from "@xyflow/react";
   import { useLiveblocksFlow, Cursors } from "@liveblocks/react-flow";
   import "@xyflow/react/dist/style.css";
   import "@liveblocks/react-ui/styles.css";
   import "@liveblocks/react-flow/styles.css";

export function Flow() {  
       const { nodes, edges, onNodesChange, onEdgesChange, onConnect, onDelete } = useLiveblocksFlow({
           suspense: true,
           nodes: {  
               initial: [
                   { id: "1", type: "input", data: { label: "Start" }, position: { x: 250, y: 0 } },
                   { id: "2", data: { label: "Process" }, position: { x: 100, y: 110 } },
                   { id: "3", type: "output", data: { label: "End" }, position: { x: 250, y: 220 } },
               ],
           },
           edges: {  
               initial: [
                   { id: "e1-2", source: "1", target: "2" },
                   { id: "e2-3", source: "2", target: "3" },
               ],
           },
       });
    
       return (    
           <div style={{ width: "100%", height: "100vh" }}>      
               <ReactFlow        
                   nodes={nodes}        
                   edges={edges}        
                   onNodesChange={onNodesChange}        
                   onEdgesChange={onEdgesChange}        
                   onConnect={onConnect}        
                   onDelete={onDelete}        
               >        
                   <Cursors />      
               </ReactFlow>    
           </div>  
       );
   }
  1. Next: authenticate and add your users

Your diagram and cursors work now, but each user is anonymous—the next step is to authenticate each user as they connect, and attach their name, color, and avatar for components like Cursors or AvatarStack.

Set up authentication and add user information.

Congratulations! You now have set up the foundation for a collaborative React Flow diagram inside your Next.js application.