How to create a collaborative code editor with CodeMirror, Yjs, Next.js, and Liveblocks

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

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

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

Download the example

On GitHub we have a working example of this collaborative CodeMirror editor for you to download and run.

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

Terminal

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.

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.

Terminal

We’ll also need another type for this tutorial. After creating the config file, open it up and insert the following:

liveblocks.config.ts

Join a Liveblocks room

Liveblocks uses the concept of rooms, separate virtual spaces where people collaborate. To create a realtime experience, multiple users must be connected to the same room. Create a file in the current directory within /app, and name it Room.tsx. Pass the location of your endpoint to LiveblocksProvider.

/app/Room.tsx

Set up the CodeMirror editor

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

Editor.tsx

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!

/app/page.tsx

Add live cursors

To add live cursors to the code editor, we can get the userInfo for the current user with useSelf, and attach it Yjs awareness. After adding the following, you should see live cursors:

Cursors.tsx

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

Add a toolbar

From this point onwards, you can build your CodeMirror app as normal! For example, should you wish to add a basic undo/redo toolbar to your app:

Toolbar.tsx

Add some matching styles:

You can then import this into your editor to enable basic CodeMirror features:

Editor.tsx

Create live avatars with Liveblocks hooks

Along with building out your code 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.

Avatars.tsx

And here’s the styles:

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

Editor.tsx

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 features! On GitHub we have a working example of this multiplayer code editor.