Sign in

Get started with a SuperDoc DOCX editor using Liveblocks 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/yjs package, and SuperDoc, a collaborative DOCX editor for the web.

Quickstart

  1. Install Liveblocks, Yjs, and SuperDoc

    Every Liveblocks package should use the same version.

    Terminal
    npm install @liveblocks/client @liveblocks/react @liveblocks/yjs yjs @superdoc-dev/react
  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
  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 SuperDoc editor

    Now that we set up Liveblocks, we can start integrating SuperDoc and Yjs in the Editor.tsx file. We use getYjsProviderForRoom to get a Yjs document and provider, then pass them to the <SuperDocEditor /> component through modules.collaboration. The component is SSR-safe and handles mounting and cleanup for you. It rebuilds when modules changes, so we memoize it.

    app/Editor.tsx
    "use client";
    import { useMemo } from "react";import { getYjsProviderForRoom } from "@liveblocks/yjs";import { useRoom, useSelf } from "@liveblocks/react/suspense";import { SuperDocEditor } from "@superdoc-dev/react";
    // Collaborative DOCX editor with live cursors, powered by SuperDocexport function Editor() { const room = useRoom(); const yProvider = getYjsProviderForRoom(room);
    // Get the current user's info from Liveblocks const userInfo = useSelf((me) => me.info); const userId = useSelf((me) => me.id);
    const modules = useMemo( () => ({ // The collaboration contract is the same for every Yjs provider: // pass the shared `ydoc` and `provider` collaboration: { ydoc: yProvider.getYDoc(), provider: yProvider }, }), [yProvider] );
    return ( <div style={{ height: "100vh" }}> <SuperDocEditor documentMode="editing" // Identify the current user for live cursors user={{ name: userInfo.name, email: userId }} modules={modules} style={{ height: "100%" }} /> </div> );}
  6. Import the SuperDoc styles

    Import the SuperDoc styles into the root layout of your app, so the editor and toolbar are styled correctly.

    app/layout.tsx
    import "@superdoc-dev/react/style.css";
  7. 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

What to read next

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


Examples using SuperDoc