Sign in

Get started with a Lexical text editor using Liveblocks Storage and React

Liveblocks is a realtime collaboration infrastructure for building performant collaborative experiences. Follow the following steps to start adding collaboration to your React 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. Set up the Liveblocks client

    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. Set up a Liveblocks client with LiveblocksProvider, and join a room with RoomProvider.

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

    App.tsx
    "use client";
    import { LiveList, LiveObject, LiveText } from "@liveblocks/client";import { LiveblocksProvider, RoomProvider,} from "@liveblocks/react/suspense";import { Editor } from "./Editor";
    export default function App() { 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(), }), ]), }), ]), }), }} > {/* ... */} </RoomProvider> </LiveblocksProvider> );}
  4. Join a Liveblocks room

    After setting up the room, you can add collaborative components inside it, using ClientSideSuspense to add loading spinners to your app.

    App.tsx
    "use client";
    import { LiveList, LiveObject, LiveText } from "@liveblocks/client";import { LiveblocksProvider, RoomProvider, ClientSideSuspense,} from "@liveblocks/react/suspense";import { Editor } from "./Editor";
    export default function App() { 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>}> <Editor /> </ClientSideSuspense> </RoomProvider> </LiveblocksProvider> );}
  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.

    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 React application.

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