Get startedGet started with Liveblocks, Lexical, 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-lexical package.

Quickstart

  1. Install Liveblocks and Lexical

    Every Liveblocks package should use the same version.

    $npm install @liveblocks/client @liveblocks/react @liveblocks/react-ui @liveblocks/react-lexical lexical @lexical/react
  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 { Editor } from "./Editor";
    export default function Page() { return ( <Room> <Editor/> </Room> );}
  5. Set up the collaborative Lexical text editor

    Now that we set up Liveblocks, we can start integrating Lexical and Liveblocks in the Editor.tsx file. To make the editor collaborative, we can use LiveblocksPlugin from @liveblocks/lexical-react. FloatingComposer is optional and adds Comments to your text editor.

    app/Editor.tsx
    "use client";
    import { LexicalComposer } from "@lexical/react/LexicalComposer";import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";import { ContentEditable } from "@lexical/react/LexicalContentEditable";import { LexicalErrorBoundary } from "@lexical/react/LexicalErrorBoundary";import { liveblocksConfig, LiveblocksPlugin,} from "@liveblocks/react-lexical";import { Toolbar } from "./Toolbar";import { Threads } from "./Threads";
    export function Editor() { // Wrap your Lexical config with `liveblocksConfig` const initialConfig = liveblocksConfig({ namespace: "Demo", onError: (error: unknown) => { console.error(error); throw error; }, });
    return ( <LexicalComposer initialConfig={initialConfig}> <Toolbar /> <div className="editor"> <RichTextPlugin contentEditable={<ContentEditable />} placeholder={<div className="placeholder">Start typing here…</div>} ErrorBoundary={LexicalErrorBoundary} /> <LiveblocksPlugin> <Threads /> </LiveblocksPlugin> </div> </LexicalComposer> );}
  6. Render threads and composer

    To add Comments to your text editor, we need to import a thread composer and list each thread on the page. Create a Threads.tsx file that uses FloatingComposer for creating new threads, alongside AnchoredThreads and FloatingThreads for displaying threads on desktop and mobile.

    app/Threads.tsx
    "use client";
    import { useThreads } from "@liveblocks/react/suspense";import { AnchoredThreads, FloatingComposer, FloatingThreads,} from "@liveblocks/react-lexical";
    export function Threads() { const { threads } = useThreads();
    return ( <> <div className="anchored-threads"> <AnchoredThreads threads={threads} /> </div> <FloatingThreads className="floating-threads" threads={threads} /> <FloatingComposer className="floating-composer" /> </> );}
  7. Create a toolbar

    To create threads with the FloatingComposer, we need to create a button that opens the composer. Create a toolbar in Toolbar.tsx with a button that opens the composer using OPEN_FLOATING_COMPOSER_COMMAND.

    app/Toolbar.tsx
    "use client";
    import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";import { OPEN_FLOATING_COMPOSER_COMMAND } from "@liveblocks/react-lexical";
    export function Toolbar() { const [editor] = useLexicalComposerContext();
    return ( <div className="toolbar"> <button onClick={() => editor.dispatchCommand(OPEN_FLOATING_COMPOSER_COMMAND, undefined) } > 💬 New comment </button> </div> );}
  8. Style your editor

    Lexical text editor is unstyled by default, so we can create some custom styles for it in a globals.css file. Import globals.css, alongside the default Liveblocks 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";import "@liveblocks/react-lexical/styles.css";import "./globals.css";
  9. Next: authenticate and add your users

    Text Editor 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, color, and avatar, to their cursors and mentions.

    Add your users to Text Editor

  10. Optional: add more features

    Lexical is a highly extensible text editor and it's possible to create complex rich-text applications. A great example is in the Lexical playground which enables features such as tables, text highlights, embedded images, and more. This is all supported using Liveblocks.

    Learn more about Lexical

What to read next

Congratulations! You now have set up the foundation for your collaborative Lexical text editor inside your Next.js application.


Examples using Lexical

We use cookies to collect data to improve your experience on our site. Read our Privacy Policy to learn more.