# Realtime collaboration

Let several people edit one document at the same time, with live changes and automatic saves to a file.

Hosts: Cloudflare, Node, Docker.

:hero-figure{name="realtime"}

```ts [server/realtime/docs.ts]
import { defineRealtime } from 'vite-hub/realtime'

// This file defines the docs room.
export default defineRealtime({
  // Only signed-in users can read and edit
  auth: true,
  // Save each document to the docs Workspace
  document: { workspace: 'docs' },
  // Let the editor save a version on request
  history: {
    checkpoint: { message: 'Save document' },
  },
})
```

```vue [src/App.vue]
<script setup lang="ts">
import { EditorContent, useEditor } from '@tiptap/vue-3'
import {
  useRealtimeTiptap,
} from 'vite-hub/realtime/vue'
import PeopleList from './components/PeopleList.vue'

// Everyone on this page edits the same file
const realtime = useRealtimeTiptap('docs', 'welcome.md')
const { people, history } = realtime
const editor = useEditor({
  extensions: realtime.extensions.value,
})
</script>

<template>
  <PeopleList :people="people" />
  <EditorContent :editor="editor" />
  <button @click="history.checkpoint()">
    Save version
  </button>
</template>
```

```vue [src/components/PeopleList.vue]
<script setup lang="ts">
import type { RealtimePerson } from 'vite-hub/realtime'

// Each person has a name and a color
defineProps<{ people: RealtimePerson[] }>()
</script>

<template>
  <ul>
    <li
      v-for="person in people"
      :key="person.clientId"
      :style="{ color: person.color }"
    >
      {{ person.name }}
    </li>
  </ul>
</template>
```

```ts [server/workspaces/docs.ts]
import { defineWorkspace } from 'vite-hub/workspace'

// The files that the docs room saves to.
// Memory is for development only.
export default defineWorkspace({
  store: { driver: 'memory' },
  rules: {
    '/**': { write: true, mediaType: 'text/markdown' },
  },
})
```

:::framework{name="Nuxt"}
```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    host: 'node',
    // Share rooms through Redis (REDIS_URL) or a Postgres Default Database
    realtime: true,
    workspace: true,
  },
})
```
:::
:::framework{name="Nitro"}
```ts [vite.config.ts]
import vue from '@vitejs/plugin-vue'
import { nitro } from 'nitro/vite'
import { vitehub } from 'vite-hub'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [
    vue(),
    nitro(),
    vitehub({
      host: 'node',
      // Share rooms through Redis (REDIS_URL) or a Postgres Default Database
      realtime: true,
      workspace: true,
    }),
  ],
})
```
:::
:::framework{name="Vite"}
```ts [vite.config.ts]
import vue from '@vitejs/plugin-vue'
import { vitehub } from 'vite-hub'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [
    vue(),
    vitehub({
      host: 'node',
      // Share rooms through Redis (REDIS_URL) or a Postgres Default Database
      realtime: true,
      workspace: true,
    }),
  ],
})
```
:::

```ts [Checkpoint] fragment
const [error, checkpoint] = await realtime.history.checkpoint()
if (error) throw error

checkpoint.document.body
checkpoint.snapshot
```

### [One file defines a room](https://vitehub.dev/docs/realtime/get-started#configure-realtime)

Add a file to `server/realtime`. Your editor connects to its name.

### [Works with your TipTap editor](https://vitehub.dev/docs/realtime/get-started#connect-a-tiptap-editor)

`useRealtimeTiptap()` gives your editor live sync and its connection state.

### [See who is editing](https://vitehub.dev/docs/realtime/server-api#carets)

Each person gets a name and a color. Everyone else sees their cursor.

### [Presence parts in @vite-hub/ui](https://vitehub.dev/docs/ui/blocks/collaborative-editor)

`people`, `status`, and the save state plug into the avatar stack and status parts.

### [Saves on its own](https://vitehub.dev/docs/realtime/configure#control-saves)

Changes save 2 seconds after the last edit, and when the last person leaves.

### [Decide who can join](https://vitehub.dev/docs/realtime/server-api#identity-and-authorization)

`auth: true` allows only signed-in people. `authorize()` decides who reads or edits each document.

### [Runs on Node or Cloudflare](https://vitehub.dev/docs/realtime/configure#host-support)

On Node, instances share rooms through Postgres or Redis. On Cloudflare, each document gets its own Durable Object.

### [Content documents edit live](https://vitehub.dev/docs/content/editing-together#live-editing)

With Realtime on, Content adds a `content` definition for your own live editor: carets, field focus, and drafts that save themselves.

Use Realtime for collaborative state that changes while clients are connected. Realtime keeps the driver, document format, and durable checkpoints explicit.

The Configure page describes the driver of each host and persistence.

On Cloudflare, rooms use Durable Objects. On Node, the `auto` driver shares rooms across instances through Redis (`REDIS_URL`) or Postgres (the client of a Postgres Default Database, or `REALTIME_POSTGRES_URL`) when the server starts, else keeps them in one process. The `memory` driver is single-process. Vercel, Netlify, and Deno have no Realtime driver.

Rooms save through a document adapter: `document: { workspace }` writes Markdown files, or pass `{ load, save }` for your own storage and throw `RealtimeDocumentConflict` on a stale `ifDigest`. A presence-only definition sets `document: false` and clients join with `connectRealtimeRoom()`. The Yjs state is the source of truth while a room is open; an external write re-seeds the room and resets clients. Use `editor` for a custom schema and async Markdown codec, `fields` for structured values, `identify()` and `authorize()` for identity and access. Inspect definitions with `vitehub inspect definitions`.

Realtime is the collaboration layer for the supported document and editor
models. It does not replace Auth, Workspace persistence, or a general WebSocket
server.

Prove two clients joining, a concurrent edit, an idle save, reconnect, and a reset after an external write. Inspect the generated registry and handler for hosted output.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/realtime.md) (this page)
- [Get started](https://vitehub.dev/raw/docs/realtime/get-started.md)
- [Configure](https://vitehub.dev/raw/docs/realtime/configure.md)
- [Server API](https://vitehub.dev/raw/docs/realtime/server-api.md)
- [Limits and errors](https://vitehub.dev/raw/docs/realtime/limits-and-errors.md)

Next: [REALTIME_CHECKPOINT_UNSUPPORTED](https://vitehub.dev/raw/docs/realtime/diagnostics/realtime_checkpoint_unsupported.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
