Get startedGet started with Liveblocks, Tiptap, 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/react-tiptap package.

Quickstart

  1. Install Liveblocks and Tiptap

    Every Liveblocks package should use the same version.

    $npm install @liveblocks/client @liveblocks/react @liveblocks/react-ui @liveblocks/react-tiptap @tiptap/react @tiptap/starter-kit
  2. Initialize the liveblocks.config.ts file

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

    $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 Tiptap text editor

    Now that we set up Liveblocks, we can start integrating Tiptap and Liveblocks in the Editor.tsx file. To make the editor collaborative, we can add useLiveblocksExtension from @liveblocks/react-tiptap.

    app/Editor.tsx
    "use client";
    import { useLiveblocksExtension } from "@liveblocks/react-tiptap";import { useEditor, EditorContent } from "@tiptap/react";import StarterKit from "@tiptap/starter-kit";import { Threads } from "./Threads";import { Toolbar } from "./Toolbar";
    export function Editor() { const liveblocks = useLiveblocksExtension();
    const editor = useEditor({ extensions: [ liveblocks, StarterKit.configure({ // The Liveblocks extension comes with its own history handling history: false, }), ], immediatelyRender: false, });
    return ( <div> <Toolbar editor={editor} /> <EditorContent editor={editor} className="editor" /> <Threads editor={editor} /> </div> );}
  6. Render threads and composer

    To add Comments to your text editor, we need to import a thread composer and list each thread on the page. Create a Threads.tsx file that uses FloatingComposer for creating new threads, alongside AnchoredThreads and FloatingThreads for displaying threads on desktop and mobile.

    app/Threads.tsx
    import { useThreads } from "@liveblocks/react/suspense";import {  AnchoredThreads,  FloatingComposer,  FloatingThreads,} from "@liveblocks/react-tiptap";import { Editor } from "@tiptap/react";
    export function Threads({ editor }: { editor: Editor | null }) { const { threads } = useThreads({ query: { resolved: false } });
    return ( <> <div className="anchored-threads"> <AnchoredThreads editor={editor} threads={threads} /> </div> <FloatingThreads editor={editor} threads={threads} className="floating-threads" /> <FloatingComposer editor={editor} className="floating-composer" /> </> );}
  7. Create a toolbar

    To create threads with the FloatingComposer, we need to create a button that opens the composer. Create a toolbar in Toolbar.tsx with a button that opens the composer using addPendingComment.

    app/Toolbar.tsx
    import { Editor } from "@tiptap/react";
    export function Toolbar({ editor }: { editor: Editor | null }) { return ( <div className="toolbar"> <button onClick={() => { editor?.chain().focus().addPendingComment().run(); }} data-active={editor?.isActive("liveblocksCommentMark")} > 💬 New comment </button> </div> );}
  8. Style your editor

    Tiptap text editor is unstyled by default, so we can create some custom styles for it in a globals.css file. Import globals.css, alongside the default Liveblocks styles. You can import them into the root layout of your app or directly into a CSS file with @import.

    app/layout.tsx
    import "@liveblocks/react-ui/styles.css";import "@liveblocks/react-tiptap/styles.css";import "./globals.css";
  9. 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

  10. Optional: add more features

    Tiptap is easy to extend, and a number of extensions are already available, making it possibly to quickly create complex rich-text applications. For example you can enable features such as tables, text highlights, embedded images, and more. This is all supported using Liveblocks.

    Find more extensions

What to read next

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


Examples using Tiptap

We use cookies to collect data to improve your experience on our site. Read our Privacy Policy to learn more.