Get started - Get started with Liveblocks, Yjs, Monaco, 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 Monaco

    $npm install @liveblocks/client @liveblocks/react @liveblocks/yjs yjs @monaco-editor/react y-monaco y-protocols
  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: "",});
  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.

    "use client";
    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 Monaco code editor

    Now that we set up Liveblocks, we can start integrating Monaco and Yjs in the Editor.tsx file. To make the editor collaborative, we can rely on MonacoBinding from y-monaco.

    "use client";
    import * as Y from "yjs";import LiveblocksProvider from "@liveblocks/yjs";import { useRoom } from "@/liveblocks.config";import { useCallback, useEffect, useState } from "react";import { Editor } from "@monaco-editor/react";import { editor } from "monaco-editor";import { MonacoBinding } from "y-monaco";import { Awareness } from "y-protocols/awareness";
    // Collaborative text editor with simple rich text, live cursors, and live avatarsexport function CollaborativeEditor() { const [editorRef, setEditorRef] = useState<editor.IStandaloneCodeEditor>(); const room = useRoom();
    // Set up Liveblocks Yjs provider and attach Monaco editor useEffect(() => { let yProvider: any; let yDoc: Y.Doc; let binding: MonacoBinding;
    if (editorRef) { yDoc = new Y.Doc(); const yText = yDoc.getText("monaco"); yProvider = new LiveblocksProvider(room, yDoc);
    // Attach Yjs to Monaco binding = new MonacoBinding( yText, editorRef.getModel() as editor.ITextModel, new Set([editorRef]), yProvider.awareness as Awareness ); }
    return () => { yDoc?.destroy(); yProvider?.destroy(); binding?.destroy(); }; }, [editorRef, room]);
    const handleOnMount = useCallback((e: editor.IStandaloneCodeEditor) => { setEditorRef(e); }, []);
    return ( <Editor onMount={handleOnMount} height="100vh" width="100hw" theme="vs-light" defaultLanguage="typescript" defaultValue="" options={{ tabSize: 2, }} /> );}
  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 Monaco code editor inside your React application.

Examples using Monaco