# 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`](/content/docs/api-reference/liveblocks-react-flow/index.html) package.

## Quickstart

1. ### Install Liveblocks and React Flow

Every Liveblocks package should use the same version.
   
   ```text
   npm install @liveblocks/client @liveblocks/react @liveblocks/react-ui @liveblocks/react-flow @xyflow/react
   ```

2. ### Initialize the `liveblocks.config.ts` file

We can use this file later to [define types for our application](/content/docs/api-reference/liveblocks-react#Typing-your-data/index.html).

```text
   npx create-liveblocks-app@latest --init --framework react
   ```

3. ### 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`](/content/docs/api-reference/liveblocks-react#LiveblocksProvider/index.html), join a room with [`RoomProvider`](/content/docs/api-reference/liveblocks-react#RoomProvider/index.html), and use [`ClientSideSuspense`](/content/docs/api-reference/liveblocks-react#ClientSideSuspense/index.html) to add a loading spinner to your app.

```tsx
   "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>  
       );
   }
   ```

4. ### 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.

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

export default function Page() {  
       return (    
           <Room>      
               <Flow />  
           </Room>  
       );
   }
   ```

5. ### Set up the collaborative React Flow diagram

Now that Liveblocks is set up, integrate React Flow in the `Flow.tsx` file. [`useLiveblocksFlow`](/content/docs/api-reference/liveblocks-react-flow#useLiveblocksFlow/index.html) keeps nodes and edges in sync across clients using Liveblocks Storage.

```tsx
   "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>  
       );
   }
   ```

6. ### Add multiplayer cursors

To show where collaborators are pointing in the diagram, render [`Cursors`](/content/docs/api-reference/liveblocks-react-flow#Cursors/index.html) inside `ReactFlow`. Also include the Liveblocks styles in addition to the React Flow ones.

```tsx
   "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>  
       );
   }
   ```

7. ### 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`](/content/docs/api-reference/liveblocks-react-flow#Cursors/index.html) or [`AvatarStack`](/content/docs/api-reference/liveblocks-react-ui#AvatarStack/index.html).

[Set up authentication and add user information](/content/docs/guides/how-to-add-users-to-liveblocks-presence-components/index.html).

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