---
meta:
  title: "Yjs"
  parentTitle: "Text editing"
  description:
    "Connect Sync to the Yjs ecosystem, shared types, awareness, and editor
    integrations."
---

Liveblocks Yjs connects a Liveblocks room to a `Y.Doc`, including Yjs shared
types and awareness. Liveblocks persists the Yjs document and synchronizes it
between clients.

<Banner title="For new text editors, use LiveText" type="info">

LiveText is our newer choice for text-editor integrations. It stores
collaborative text directly in Storage, is much more efficient, and works with
different editor adapters. Read [text editing](/docs/products/sync/text-editing)
to compare the two approaches.

</Banner>

## Y.Doc

[`Y.Doc`](https://docs.yjs.dev/api/y.doc) is the core data structure used by
Yjs. It is a JSON-like object that represents the state of a collaborative
document. It is used to store the content of the document, and to synchronize it
between clients. In Liveblocks, get the current room’s `Y.Doc` with
[`getYjsProviderForRoom`](/docs/api-reference/liveblocks-yjs#getYjsProviderForRoom).

```ts
import { useRoom } from "@liveblocks/react";
import { getYjsProviderForRoom } from "@liveblocks/yjs";

function App() {
  const room = useRoom();
  // +++
  const yProvider = getYjsProviderForRoom(room);
  const yDoc = yProvider.getYDoc();
  // +++

  // ...
}
```

## Shared types

[Shared types](https://docs.yjs.dev/api/shared-types) in Yjs are CRDT-backed
data structures (like maps, arrays, and text) stored inside a `Y.Doc` that
automatically synchronize and merge changes across multiple clients without
conflicts.

```tsx
import { useRoom } from "@liveblocks/react";
import { getYjsProviderForRoom } from "@liveblocks/yjs";

function App() {
  const room = useRoom();
  const yProvider = getYjsProviderForRoom(room);
  const yDoc = yProvider.getYDoc();

  // +++
  const insertText = useCallback(() => {
    const yText = yDoc.getText("editor"); // Y.Text
    yText.insert(0, "Hello collaborative world");
  }, []);
  // +++

  // +++
  const changeTheme = useCallback(() => {
    const yMap = yDoc.getMap("settings"); // Y.Map
    yMap.set("theme", "dark");
  }, []);
  // +++

  // ...
}
```

## Version history

Create version snapshots of your Yjs document, and let users browse, preview,
and restore them.
[`useHistoryVersions`](/docs/api-reference/liveblocks-react#useHistoryVersions)
lists a room’s versions, and
[`useHistoryVersionYjsData`](/docs/api-reference/liveblocks-react#useHistoryVersionYjsData)
returns a version as a binary Yjs update, which you can apply to a fresh `Y.Doc`
to read its contents.

```tsx
import {
  useHistoryVersions,
  useHistoryVersionYjsData,
} from "@liveblocks/react";
import * as Y from "yjs";

function VersionHistory() {
  // +++
  const { versions } = useHistoryVersions();
  // +++

  return (
    <div>
      // +++
      {versions.map((version) => (
        <div key={version.id}>
          <time>{version.createdAt}</time>
          <VersionPreview versionId={version.id} />
        </div>
      ))}
      // +++
    </div>
}

function VersionPreview({ versionId }) {
  // +++
  const { data } = useHistoryVersionYjsData(versionId);
  // +++

  // Apply the version's binary update to an empty Y.Doc
  const yDoc = new Y.Doc();
  Y.applyUpdate(yDoc, data);

  // ...
}
```

If you’re using our Tiptap, BlockNote, or Lexical plugins, ready-made
[`HistoryVersionPreview`](/docs/api-reference/liveblocks-react-tiptap#HistoryVersionPreview)
components preview and restore versions for you. Learn how to set this up in our
[version history guide](/docs/guides/how-to-add-version-history-to-your-app).

## Get started

Choose a starting point for your Yjs-backed editor.

<ListGrid columns={2} defaultVisibleItems={4}>
  <DocsCard
    type="technology"
    title="Get started with SuperDoc"
    href="/docs/get-started/yjs-superdoc-react"
    description="Yjs-backed document editor with React"
    visual={<DocsReactIcon />}
  />
  <DocsCard
    type="technology"
    title="Get started with Monaco"
    href="/docs/get-started/yjs-monaco-react"
    description="Yjs-backed code editor with React"
    visual={<DocsReactIcon />}
  />
  <DocsCard
    type="technology"
    title="Get started with Slate"
    href="/docs/get-started/yjs-slate-react"
    description="Yjs-backed text editor with React"
    visual={<DocsReactIcon />}
  />
  <DocsCard
    type="technology"
    title="Get started with Quill"
    href="/docs/get-started/yjs-quill-react"
    description="Yjs-backed text editor with React"
    visual={<DocsReactIcon />}
  />
  <DocsCard
    type="technology"
    title="Get started with CodeMirror"
    href="/docs/get-started/yjs-codemirror-react"
    description="Yjs-backed code editor with React"
    visual={<DocsReactIcon />}
  />
  <DocsCard
    type="technology"
    title="Get started with Tiptap and Yjs"
    href="/docs/get-started/nextjs-tiptap"
    description="Yjs-backed text editor with Next.js"
    visual={<DocsNextjsIcon />}
  />
  <DocsCard
    type="technology"
    title="Get started with Lexical and Yjs"
    href="/docs/get-started/nextjs-lexical"
    description="Yjs-backed text editor with Next.js"
    visual={<DocsNextjsIcon />}
  />
</ListGrid>

### How-to guides

<ListGrid columns={2}>
  <DocsCard
    type="technology"
    title="Yjs guides"
    href="/docs/guides?technologies=yjs"
    description="All how-to guides for Yjs"
    visual={<DocsBooksIcon className="fill-product-icon-brand h-auto w-6" />}
  />
  <DocsCard
    type="technology"
    title="Yjs best practices"
    href="/docs/guides/yjs-best-practices-and-tips"
    description="Tips for building with Yjs"
    visual={<DocsBookIcon className="fill-product-icon-brand h-auto w-6" />}
  />
  <DocsCard
    type="technology"
    title="Use Y.Doc on the server"
    href="/docs/guides/how-to-use-your-ydoc-on-the-server"
    description="Access Yjs data from your back end"
    visual={<DocsBookIcon className="fill-product-icon-brand h-auto w-6" />}
  />
  <DocsCard
    type="technology"
    title="Use subdocuments"
    href="/docs/guides/how-to-use-yjs-subdocuments"
    description="Split data into subdocuments"
    visual={<DocsBookIcon className="fill-product-icon-brand h-auto w-6" />}
  />
</ListGrid>

### API Reference

<ListGrid columns={2}>
  {" "}
  <DocsCard
    type="technology"
    title="Yjs"
    href="/docs/api-reference/liveblocks-yjs"
    description="@liveblocks/yjs"
    visual={<DocsYjsIcon className="fill-product-icon-brand h-auto w-6" />}
  />
  <DocsCard
    type="technology"
    title="Node.js"
    href="/docs/api-reference/liveblocks-node#Yjs"
    description="@liveblocks/node"
    visual={<DocsNodejsIcon className="fill-product-icon-brand h-auto w-6" />}
  />
</ListGrid>

### Examples

<ListGrid columns={2}>
  <ExampleCard
    example={{
      title: "Collaborative Text Editor (SuperDoc)",
      slug: "collaborative-text-editor/nextjs-yjs-superdoc",
      image: "/images/examples/thumbnails/text-editor.jpg",
    }}
    technologies={["nextjs"]}
    openInNewWindow
  />
  <ExampleCard
    example={{
      title: "Collaborative Code Editor",
      slug: "collaborative-code-editor",
      image: "/images/examples/thumbnails/code-editor.jpg",
      advanced: true,
    }}
    technologies={["nextjs"]}
    openInNewWindow
  />
  <ExampleCard
    example={{
      title: "Collaborative Text Editor (Slate)",
      slug: "collaborative-text-editor/nextjs-yjs-slate",
      image: "/images/examples/thumbnails/text-editor.jpg",
    }}
    technologies={["nextjs"]}
    openInNewWindow
  />
  <ExampleCard
    example={{
      title: "Collaborative Text Editor (Quill)",
      slug: "collaborative-text-editor/nextjs-yjs-quill",
      image: "/images/examples/thumbnails/text-editor.jpg",
    }}
    technologies={["nextjs"]}
    openInNewWindow
  />
</ListGrid>

---

For an overview of all available documentation, see [/llms.txt](/llms.txt).
