Get started with a CodeMirror code 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/codemirror package.
Quickstart
Install Liveblocks and CodeMirror
Every Liveblocks package should use the same version.
TerminalInitialize the
liveblocks.config.tsfileWe can use this file later to define types for our application.
TerminalAdd types for your CodeMirror document and presence:
liveblocks.config.tsCreate 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’
/approuter, we recommend creating your room in aRoom.tsxfile in the same directory as your current route.Store your editor document in Storage as
LiveText, and set an initial presence shape for cursors.Set up a Liveblocks client with
LiveblocksProvider, join a room withRoomProvider, and useClientSideSuspenseto add a loading spinner to your app.app/Room.tsxAdd the Liveblocks room to your page
After creating your room file, import it into your
page.tsxfile and place your editor inside it.app/page.tsxSet up the collaborative CodeMirror editor
Now that Liveblocks is set up, create a CodeMirror editor in
Editor.tsx. UsecreateLiveblocksSyncPluginto sync the document with Storage, andcreateLiveblocksPresencePluginto show remote carets and selections.app/Editor.tsxStyle remote carets and selections
The presence plugin renders remote carets and selections using the
.lb-remote-caretand.lb-remote-selectionclass names. Add styles for them in your CSS. This package does not ship a stylesheet.app/globals.cssapp/layout.tsxNext: authenticate and add your users
Your 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 and color to remote carets.
Set up authentication and add user information
What to read next
Congratulations! You now have set up the foundation for your collaborative CodeMirror editor inside your Next.js application.
If you prefer to use Yjs with CodeMirror, see the Yjs CodeMirror React quickstart.