Sign in

Get started with a Lexical text editor using Liveblocks Storage 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/lexical package.

This guide uses Liveblocks Storage. For Comments, mentions, and the full Text Editor product, see the @liveblocks/react-lexical quickstart instead.

Quickstart

  1. Install Liveblocks and Lexical

    Every Liveblocks package should use the same version.

    Terminal
    npm install @liveblocks/client @liveblocks/react @liveblocks/lexical lexical @lexical/react @lexical/selection @lexical/utils
  2. Initialize the liveblocks.config.ts file

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

    Terminal
    npx create-liveblocks-app@latest --init --framework react

    Add types for your Lexical document and presence:

    liveblocks.config.ts
    import type { LiveLexicalSelection, LiveRootNode } from "@liveblocks/lexical";
    declare global { interface Liveblocks { Presence: { selection: LiveLexicalSelection | null; }; Storage: { document: LiveRootNode; }; UserMeta: { id?: string; info?: { name?: string; color?: string; }; }; }}
    export {};
  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.

    Store your editor document as a Storage tree and set an initial presence shape for cursors.

    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 { LiveList, LiveObject, LiveText } from "@liveblocks/client";import { LiveblocksProvider, RoomProvider, ClientSideSuspense,} from "@liveblocks/react/suspense";
    export function Room({ children }: { children: ReactNode }) { return ( <LiveblocksProvider publicApiKey={""}> <RoomProvider id="my-room" initialPresence={{ selection: null }} initialStorage={{ document: new LiveObject({ kind: "root", type: "root", version: 1, children: new LiveList([ new LiveObject({ kind: "element", type: "paragraph", version: 1, children: new LiveList([ new LiveObject({ kind: "text", type: "text", version: 1, content: new LiveText(), }), ]), }), ]), }), }} > <ClientSideSuspense fallback={<div>Loading…</div>}> {children} </ClientSideSuspense> </RoomProvider> </LiveblocksProvider> );}
  4. Add the Liveblocks room to your page

    After creating your room file, import it into your page.tsx file and place your editor 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 editor

    Now that Liveblocks is set up, create a Lexical editor in Editor.tsx. Use LiveblocksCollaborationPlugin to sync the document with Storage, and RemoteCursorsPlugin to show remote carets and selections.

    app/Editor.tsx
    "use client";
    import { useCallback, useSyncExternalStore } from "react";import { LexicalComposer } from "@lexical/react/LexicalComposer";import { ContentEditable } from "@lexical/react/LexicalContentEditable";import { LexicalErrorBoundary } from "@lexical/react/LexicalErrorBoundary";import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";import { LiveblocksCollaborationPlugin, RemoteCursorsPlugin,} from "@liveblocks/lexical";import type { Room } from "@liveblocks/client";import { useRoom } from "@liveblocks/react/suspense";import "@liveblocks/lexical/styles.css";
    export function Editor() { const room = useRoom(); const root = useRoot(room);
    if (root === null) { return <div>Loading…</div>; }
    const document = root.get("document");
    return ( <LexicalComposer initialConfig={{ namespace: "Liveblocks", onError: (error) => console.error(error), }} > <div className="relative"> <RichTextPlugin contentEditable={<ContentEditable className="outline-none" />} ErrorBoundary={LexicalErrorBoundary} /> <LiveblocksCollaborationPlugin root={document}> <RemoteCursorsPlugin /> </LiveblocksCollaborationPlugin> </div> </LexicalComposer> );}
    function useRoot(room: Room) { const subscribe = room.events.storageDidLoad.subscribeOnce; const getSnapshot = room.getStorageOrNull; const getServerSnapshot = useCallback(() => null, []); return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);}
  6. Next: authenticate and add your users

    Your 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 and color to remote carets.

    Set up authentication and add user information

What to read next

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

For Comments, mentions, and default Text Editor components, see @liveblocks/react-lexical.