Get started - Get started with Liveblocks, Lexical, Yjs, and React

Liveblocks is a real-time 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/yjs package.


  1. Install Liveblocks, Yjs, and Lexical

    $npm install @liveblocks/client @liveblocks/react @liveblocks/yjs yjs lexical @lexical/react @lexical/yjs
  2. Initialize the liveblocks.config.ts file

    $npx create-liveblocks-app@latest --init --framework react
  3. Set up the Liveblocks client

    The first step in connecting to Liveblocks is creating a client which will be responsible for communicating with the back end. You can do this by modifying createClient in your config file, and passing your public API key.

    const client = createClient({  publicApiKey: "pk_prod_xxxxxxxxxxxxxxxxxxxxxxxx",});
  4. Join a Liveblocks room

    Liveblocks uses the concept of rooms, separate virtual spaces where people collaborate. To create a real-time experience, multiple users must be connected to the same room.

    import { RoomProvider } from "./liveblocks.config";import { Editor } from "./Editor";import { ClientSideSuspense } from "@liveblocks/react";
    export default function Page() { return ( <RoomProvider id="my-room" initialPresence={{}}> <ClientSideSuspense fallback="Loading…"> {() => <Editor />} </ClientSideSuspense> </RoomProvider> );}
  5. Set up the collaborative Lexical text editor

    Now that we set up Liveblocks, we can start integrating Lexical and Yjs in the Editor.tsx file. To make the editor collaborative, we can rely on the CollaborationPlugin from @lexical/react/LexicalCollaborationPlugin.

    import { useRoom } from "../liveblocks.config";import * as Y from "yjs";import LiveblocksProvider from "@liveblocks/yjs";import {  $getRoot,  $createParagraphNode,  $createTextNode,  LexicalEditor,} from "lexical";import { LexicalComposer } from "@lexical/react/LexicalComposer";import { ContentEditable } from "@lexical/react/LexicalContentEditable";import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";import { CollaborationPlugin } from "@lexical/react/LexicalCollaborationPlugin";import LexicalErrorBoundary from "@lexical/react/LexicalErrorBoundary";import { Provider } from "@lexical/yjs";
    function initialEditorState(editor: LexicalEditor): void { const root = $getRoot(); const paragraph = $createParagraphNode(); const text = $createTextNode(); paragraph.append(text); root.append(paragraph);}
    export default Editor() { const room = useRoom();
    // Lexical config const initialConfig = { // Don’t set default state, allow CollaborationPlugin to do it instead editorState: null, namespace: "Demo", nodes: [], theme: {}, onError: (error: unknown) => throw error, };
    return ( <LexicalComposer initialConfig={initialConfig}> <RichTextPlugin contentEditable={<ContentEditable />} placeholder={ <div>Start typing here…</div> } ErrorBoundary={LexicalErrorBoundary} /> <CollaborationPlugin id="yjs-plugin" providerFactory={(id, yDocMap) => { const yDoc = new Y.Doc(); yDocMap.set(id, yDoc); return new LiveblocksProvider(room, yDoc) as Provider; }} initialEditorState={initialEditorState} shouldBootstrap={true} /> </LexicalComposer> );}
  6. Next: set up authentication

    By default, Liveblocks is configured to work without an authentication endpoint where everyone automatically has access to rooms. This approach is great for prototyping and marketing pages where setting up your own security isn’t always required. If you want to limit access to a room for certain users, you’ll need to set up an authentication endpoint to enable permissions.

    Set up authentication

What to read next

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

Examples using Lexical