--- meta: title: "Get started with a CodeMirror code editor using Liveblocks and Next.js" parentTitle: "Quickstart" description: "Learn how to install 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`](/docs/api-reference/liveblocks-codemirror) package. The CodeMirror LiveText integration is currently in beta, and LiveText documents are limited to 2 MB. Read the [LiveText vs Yjs](/docs/guides/livetext-vs-yjs) guide to compare both approaches. ## Quickstart Install Liveblocks and CodeMirror Every Liveblocks package should use the same version. ```bash trackEvent="install_liveblocks" npm install @liveblocks/client @liveblocks/react @liveblocks/codemirror codemirror ``` Initialize the `liveblocks.config.ts` file We can use this file later to [define types for our application](/docs/api-reference/liveblocks-react#Typing-your-data). ```bash npx create-liveblocks-app@latest --init --framework react ``` Add types for your CodeMirror document and Presence: ```ts file="liveblocks.config.ts" import type { LiveblocksCodemirrorSelection } from "@liveblocks/codemirror"; import { LiveText } from "@liveblocks/client"; declare global { interface Liveblocks { Presence: { selection: LiveblocksCodemirrorSelection | null; }; Storage: { document: LiveText; }; UserMeta: { id?: string; info?: { name?: string; color?: string; }; }; } } export {}; ``` 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. Store your editor document in Storage as [`LiveText`](/docs/api-reference/liveblocks-client#LiveText), and set an initial Presence shape for cursors. Set up a Liveblocks client with [`LiveblocksProvider`](/docs/api-reference/liveblocks-react#LiveblocksProvider), join a room with [`RoomProvider`](/docs/api-reference/liveblocks-react#RoomProvider), and use [`ClientSideSuspense`](/docs/api-reference/liveblocks-react#ClientSideSuspense) to add a loading spinner to your app. ```tsx file="app/Room.tsx" "use client"; import { ReactNode } from "react"; import { LiveText } from "@liveblocks/client"; import { LiveblocksProvider, RoomProvider, ClientSideSuspense, } from "@liveblocks/react/suspense"; export function Room({ children }: { children: ReactNode }) { return ( // +++ Loading…}> {children} // +++ ); } ``` Add the Liveblocks room to your page After creating your room file, import it into your `page.tsx` file and place your editor inside it. ```tsx file="app/page.tsx" import { Room } from "./Room"; import { Editor } from "./Editor"; export default function Page() { return ( // +++ // +++ ); } ``` Set up the collaborative CodeMirror editor Now that Liveblocks is set up, create a CodeMirror editor in `Editor.tsx`. Use [`createLiveblocksSyncPlugin`](/docs/api-reference/liveblocks-codemirror#createLiveblocksSyncPlugin) to sync the document with Storage, and [`createLiveblocksPresencePlugin`](/docs/api-reference/liveblocks-codemirror#createLiveblocksPresencePlugin) to show remote carets and selections. ```tsx file="app/Editor.tsx" "use client"; import { useEffect, useRef } from "react"; import { EditorView } from "@codemirror/view"; import { EditorState } from "@codemirror/state"; import { createLiveblocksPresencePlugin, createLiveblocksSyncPlugin, } from "@liveblocks/codemirror"; import { useMutableStorage, useRoom } from "@liveblocks/react/suspense"; export function Editor() { const room = useRoom(); const text = useMutableStorage().get("document"); const containerRef = useRef(null); useEffect(() => { if (containerRef.current === null) return; const view = new EditorView({ parent: containerRef.current, state: EditorState.create({ // +++ doc: text.toString(), extensions: [ createLiveblocksSyncPlugin(room, text), createLiveblocksPresencePlugin(room, text), ], // +++ }), }); return () => { view.destroy(); }; }, [room, text]); return
; } ``` Style remote carets and selections The Presence plugin renders remote carets and selections using the `.lb-remote-caret` and `.lb-remote-selection` class names. Add styles for them in your CSS. This package does not ship a stylesheet. ```css file="app/globals.css" .editor { height: 100vh; } .lb-remote-selection { position: absolute; background-color: color-mix(in srgb, var(--lb-remote-color) 25%, transparent); border-radius: 1px; pointer-events: none; box-sizing: border-box; } .lb-remote-caret { position: absolute; width: 0; border-left: 2px solid var(--lb-remote-color); pointer-events: none; box-sizing: border-box; } ``` ```tsx file="app/layout.tsx" import "./globals.css"; ``` Next: 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. ## What to read next Congratulations! You now have set up the foundation for your collaborative CodeMirror editor inside your Next.js application. - [@liveblocks/codemirror API Reference](/docs/api-reference/liveblocks-codemirror) - [Next.js and React guides](/docs/guides?technologies=nextjs%2Creact) - [CodeMirror website](https://codemirror.net) If you prefer to use Yjs with CodeMirror, see the [Yjs CodeMirror React quickstart](/docs/get-started/yjs-codemirror-react).