How to create a collaborative text editor with Tiptap, Yjs, Next.js, and Liveblocks

In this tutorial, we’ll be building a collaborative text editor using Tiptap, Yjs, Next.js, and Liveblocks.

This guide assumes that you’re already familiar with React, Next.js, TypeScript, and Tiptap.

Install Tiptap, Yjs, and Liveblocks into your Next.js application

Run the following command to install the Tiptap, Yjs, and Liveblocks packages:

$npm install @liveblocks/client @liveblocks/react @liveblocks/yjs yjs @tiptap/extension-collaboration @tiptap/extension-collaboration-cursor @tiptap/pm @tiptap/react @tiptap/starter-kit

Set up access token authentication

The first step in connecting to Liveblocks is to set up an authentication endpoint in /app/api/liveblocks-auth/route.ts.

import { Liveblocks } from "@liveblocks/node";import { NextRequest } from "next/server";
const API_KEY = "";
const liveblocks = new Liveblocks({ secret: API_KEY!,});
export async function POST(request: NextRequest) { // Get the current user's info from your database const user = { id: "", info: { name: "Charlie Layne", color: "#D583F0", picture: "", }, };
// Create a session for the current user // userInfo is made available in Liveblocks presence hooks, e.g. useOthers const session = liveblocks.prepareSession(, { userInfo:, });
// Give the user access to the room const { room } = await request.json(); session.allow(room, session.FULL_ACCESS);
// Authorize the user and return the result const { body, status } = await session.authorize(); return new Response(body, { status });}

Here’s an example using the older API routes format in /pages.

Initialize your Liveblocks config file

Let’s initialize the liveblocks.config.ts file in which you’ll set up the Liveblocks client.

$npx create-liveblocks-app@latest --init --framework react

Set up the client

Next, we can create the front end client which will be responsible for communicating with the back end. You can do this by modifying createClient in your config file, and passing the location of your endpoint.

const client = createClient({  authEndpoint: "/api/liveblocks-auth",});

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. Create a file in the current directory within /app, and name it Room.tsx.

"use client";
import { ReactNode } from "react";import { RoomProvider } from "../liveblocks.config";import { ClientSideSuspense } from "@liveblocks/react";
export function Room({ children }: { children: ReactNode }) { return ( <RoomProvider id={roomId} initialPresence={{ cursor: null, }} > <ClientSideSuspense fallback={<div>Loading…</div>}> {() => children} </ClientSideSuspense> </RoomProvider> );}

Set up the Tiptap editor

Now that we’ve set up Liveblocks, we can start integrating Tiptap and Yjs in the Editor.tsx file.

"use client";
import { useEditor, EditorContent } from "@tiptap/react";import StarterKit from "@tiptap/starter-kit";import Collaboration from "@tiptap/extension-collaboration";import CollaborationCursor from "@tiptap/extension-collaboration-cursor";import * as Y from "yjs";import LiveblocksProvider from "@liveblocks/yjs";import { useRoom } from "@/liveblocks.config";import { useEffect, useState } from "react";import styles from "./CollaborativeEditor.module.css";
// Collaborative text editor with simple rich text, live cursors, and live avatarsexport function CollaborativeEditor() { const room = useRoom(); const [doc, setDoc] = useState<Y.Doc>(); const [provider, setProvider] = useState<any>();
// Set up Liveblocks Yjs provider useEffect(() => { const yDoc = new Y.Doc(); const yProvider = new LiveblocksProvider(room, yDoc); setDoc(yDoc); setProvider(yProvider);
return () => { yDoc?.destroy(); yProvider?.destroy(); }; }, [room]);
if (!doc || !provider) { return null; }
return <TiptapEditor doc={doc} provider={provider} />;}
type EditorProps = { doc: Y.Doc; provider: any;};
function TiptapEditor({ doc, provider }: EditorProps) { // Set up editor with plugins const editor = useEditor({ editorProps: { attributes: { // Add styles to editor element class: styles.editor, }, }, extensions: [ StarterKit.configure({ // The Collaboration extension comes with its own history handling history: false, }), // Register the document with Tiptap Collaboration.configure({ document: doc, }), // Attach provider and user info CollaborationCursor.configure({ provider: provider, }), ], });
return ( <div className={styles.container}> <EditorContent editor={editor} className={styles.editorContainer} /> </div> );}

And here is the Editor.module.css file to make sure your multiplayer text editor looks nice and tidy.

Add your editor to the current page

Next, add the CollaborativeEditor into the page file, and place it inside the Room component we created earlier. We should now be seeing a basic collaborative editor!

import { Room } from "./Room";import CollaborativeEditor from "@/components/Editor";
export default function Page() { return ( <Room> <CollaborativeEditor /> </Room> );}

Add live cursors

To add live cursors to the text editor, we can get the userInfo for the current user with useSelf, and feed it into editor. We should now see some cursors with names.

import { useSelf } from "../liveblocks.config.ts";// ...
function TiptapEditor({ doc, provider }: EditorProps) { // Get user info from Liveblocks authentication endpoint const userInfo = useSelf((me) =>;
// Set up editor with plugins, and place user info into Yjs awareness and cursors const editor = useEditor({ // ... extensions: [ // ...
// Attach provider and user info CollaborationCursor.configure({ provider: provider, user: userInfo, }), ], });
return ( <div className={styles.container}> <EditorContent editor={editor} className={styles.editorContainer} /> </div> );}

We can style these cursors by placing CSS in a global CSS file.

Add a toolbar

From this point onwards, you can build your Tiptap app as normal! For example, should you wish to add a basic text-style toolbar to your app:

import { Editor } from "@tiptap/react";import styles from "./Toolbar.module.css";
type Props = { editor: Editor | null;};
export function Toolbar({ editor }: Props) { if (!editor) { return null; }
return ( <div className={styles.toolbar}> <button className={styles.button} onClick={() => editor.chain().focus().toggleBold().run()} disabled={!editor.can().chain().focus().toggleBold().run()} data-active={editor.isActive("bold") ? "is-active" : undefined} aria-label="bold" > B </button> <button className={styles.button} onClick={() => editor.chain().focus().toggleItalic().run()} disabled={!editor.can().chain().focus().toggleItalic().run()} data-active={editor.isActive("italic") ? "is-active" : undefined} aria-label="italic" > i </button> <button className={styles.button} onClick={() => editor.chain().focus().toggleStrike().run()} disabled={!editor.can().chain().focus().toggleStrike().run()} data-active={editor.isActive("strike") ? "is-active" : undefined} aria-label="strikethrough" > S </button> </div> );}

Add some matching styles:

You can then import this into your editor to enable basic rich-text:

import { Toolbar } from "./Toolbar";// ...
function TiptapEditor({ doc, provider }: EditorProps) { // ...
return ( <div className={styles.container}> <div className={styles.editorHeader}> <Toolbar editor={editor} /> </div> <EditorContent editor={editor} className={styles.editorContainer} /> </div> );}

Create live avatars with Liveblocks hooks

Along with building out your text editor, you can now use other Liveblocks features, such as Presence. The useOthers hook allows us to view information about each user currently online, and we can turn this into a live avatars component.

import { useOthers, useSelf } from "@/liveblocks.config";import styles from "./Avatars.module.css";
export function Avatars() { const users = useOthers(); const currentUser = useSelf();
return ( <div className={styles.avatars}> {{ connectionId, info }) => { return ( <Avatar key={connectionId} picture={info.picture} name={} /> ); })}
{currentUser && ( <div className="relative ml-8 first:ml-0"> <Avatar picture={} name={} /> </div> )} </div> );}
export function Avatar({ picture, name }: { picture: string; name: string }) { return ( <div className={styles.avatar} data-tooltip={name}> <img src={picture} className={styles.avatar_picture} data-tooltip={name} /> </div> );}

And here’s the styles:

You can then import this to your editor to see it in action:

import { Avatars } from "./Avatars";// ...
function TiptapEditor({ doc, provider }: EditorProps) { // ...
return ( <div className={styles.container}> <div className={styles.editorHeader}> <Toolbar editor={editor} /> <Avatars /> </div> <EditorContent editor={editor} className={styles.editorContainer} /> </div> );}

Note that the cursors and avatars match in color and name, as the info for both is sourced from the Liveblocks authentication endpoint.

Try it out

You should now see the complete editor, along with live cursors, live avatars, and some basic rich-text features! On GitHub we have a working example of this multiplayer text editor.