Get startedGet started with Comments using Liveblocks and Next.js

Liveblocks is a realtime collaboration infrastructure for building performant collaborative experiences. Follow the following steps to start adding a commenting experience to your Next.js /app directory application using the hooks from @liveblocks/react and the components from @liveblocks/react-ui.

Quickstart

  1. Install Liveblocks

    Every package should use the same version.

    $npm install @liveblocks/client @liveblocks/react @liveblocks/react-ui
  2. 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
  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, join a room with RoomProvider, and use ClientSideSuspense to add a loading spinner to your app.

    app/Room.tsx
    "use client";
    import { ReactNode } from "react";import { LiveblocksProvider, RoomProvider, ClientSideSuspense,} from "@liveblocks/react/suspense";
    export function Room({ children }: { children: ReactNode }) { return ( <LiveblocksProvider publicApiKey={""}> <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.

    app/page.tsx
    import { Room } from "./Room";import { CollaborativeApp } from "./CollaborativeApp";
    export default function Page() { return ( <Room> <CollaborativeApp /> </Room> );}
  5. Use the Liveblocks hooks and components

    Now that we’re connected to a room, we can start using the Liveblocks hooks and components. We’ll add useThreads to get the threads in the room, then we’ll use the Thread component to render them. Finally, we’ll add a way to create threads by adding a Composer.

    app/CollaborativeApp.tsx
    "use client";
    import { useThreads } from "@liveblocks/react/suspense";import { Composer, Thread } from "@liveblocks/react-ui";
    export function CollaborativeApp() { const { threads } = useThreads();
    return ( <div> {threads.map((thread) => ( <Thread key={thread.id} thread={thread} /> ))} <Composer /> </div> );}
  6. Import default styles

    The default components come with default styles, you can import them into the root layout of your app or directly into a CSS file with @import.

    app/layout.tsx
    import "@liveblocks/react-ui/styles.css";
  7. Next: authenticate and add your users

    Comments is set up and working now, but each user is anonymous—the next step is to authenticate each user as they connect, and attach their name and avatar to their comments.

    Add your users to Comments

What to read next

Congratulations! You’ve set up the foundation to start building a commenting experience for your Next.js application.


Examples using Next.js