# Realtime server API

## Public imports and generated output

| Import | Use |
| --- | --- |
| `defineRealtime` from `vite-hub/realtime` | Declare a room file in `server/realtime`. |
| `defineRealtimeEditor`, `realtimeMarkdownEditor` from `vite-hub/realtime` | Declare an editor schema and Markdown codec, or use the built-in kit. |
| `RealtimeDocumentConflict` from `vite-hub/realtime` | Throw from a document adapter when `ifDigest` no longer matches. |
| `realtimeDocumentLayout`, `readRealtimeStatus` from `vite-hub/realtime` | Read a room document's `body`, `fields`, and `status` root types. |
| `useRealtimeTiptap` from `vite-hub/realtime/vue` | Connect a Vue TipTap editor, fields, presence, saves, and resets. |
| `connectRealtimeRoom` from `@vite-hub/realtime/client` | Join one room for [presence without an editor](https://vitehub.dev/docs/realtime/server-api#presence-without-an-editor), in any framework. |
| `refreshRealtimeDocument` from `vite-hub/realtime/server` | Tell a document's open room that its stored copy changed. |
| `isRealtimeAvailable` from `vite-hub/realtime/server` | Check if this build has a room authority ([Builds without Realtime](https://vitehub.dev/docs/realtime/configure#builds-without-realtime)). |
| `workspaceDocument` from `vite-hub/realtime/server` | The Workspace document adapter, to wrap or combine. |
| `blobDocument` from `vite-hub/realtime/server` | A document adapter that stores each document as one private Blob file ([Blob documents](https://vitehub.dev/docs/realtime/server-api#blob-documents)). |
| `createRealtimeHandler` from `vite-hub/realtime/server` | Build a handler for a manual server setup. ViteHub generates this route for normal apps. |

ViteHub generates `.vitehub/nitro/realtime/registry.mjs` and
`.vitehub/nitro/realtime/handler.ts`. Read them to inspect the build, but do
not import them.

## How a room keeps state

While a room is open, its Yjs state is the source of truth. Realtime loads
the stored document once, when the room opens, and then only saves to it. It
never rebuilds a Yjs document from Markdown for clients that already have
state, because merging two separately built documents duplicates content.

When the stored document changes outside the room, Realtime re-seeds the room
from the new version and resets every client. A reset closes the connection.
The Vue composable drops its Y.Doc, reports the reset, and reconnects with an
empty one.

## Custom document adapters

`document: { workspace: 'docs' }` stores each path as a Markdown file in a
Workspace working tree. To store documents elsewhere, pass an adapter object.
Realtime calls it with plain snapshots. Your storage stays a copy synced from
the room.

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

export default defineRealtime({
  document: {
    kind: 'pages',
    async load({ documentId }) {
      const page = await db.pages.find(documentId)
      if (!page) return { body: '', digest: null }
      return { body: page.body, digest: page.version, fields: { title: page.title } }
    },
    async save({ documentId }, { body, fields }, { ifDigest, user }) {
      const version = await db.pages.update(documentId, { body, title: fields.title, editedBy: user?.id }, { ifVersion: ifDigest })
      if (!version) throw new RealtimeDocumentConflict()
      return { digest: version, issues: fields.title ? [] : [{ field: 'title', message: 'Add a title', path: [] }] }
    },
  },
})
```

| Member | Contract |
| --- | --- |
| `load(context)` | Returns `{ body, fields?, digest }`. `digest: null` means the document does not exist yet. Called when a room opens and on `refresh()`. |
| `save(context, { body, fields }, options)` | Stores the snapshot only if the stored digest equals `options.ifDigest`. Else throws `RealtimeDocumentConflict` (a Workspace conflict also counts). Returns `{ digest, issues?, stored? }`. |
| `stored` | The stored form when the adapter normalized the snapshot. Realtime merges the difference into the room as a structural diff, with no reset, and keeps concurrent edits. Omit `fields` to leave live fields as they are. |
| `issues` | Validation problems that did not block the save. They show in the room `status` map. |
| `checkpoint(context, { digest, message, user })` | Optional. Records a history entry that must contain `digest`. Enables `history.checkpoint()`. |
| `hold(context, { users })` | Optional. Called while a writable room has clients: when one joins or leaves, and every 60 seconds. `users` are the identified people connected now. Use it to hold a lease. A thrown error shows in the room `status` until a later call works. |
| `release(context)` | Optional. Called once after the last writer left a room that `hold()` kept, after the room saved. |
| `kind` | Optional label for `vitehub inspect`. |

`context` is `{ authority, definition, documentId }`. `authority` is `memory`
or `cloudflare`, so an adapter can refuse a temporary store behind a durable
room. `options` is `{ ifDigest, reason, user, editors }`:

- `reason` is `idle`, `leave`, or `request`.
- `user` is the latest identified editor.
- `editors` lists every identified user who changed the room since the last
  save.

Adapters run inside the room authority. On Cloudflare that is the Durable
Object, so bindings come from its environment.

### Blob documents

`blobDocument(store, { prefix, contentType })` stores each document's body as
one private Blob file at `prefix + documentId`. The digest is the SHA-256 of
the body. `contentType` defaults to `text/markdown; charset=utf-8`. A `%` in
the document id stays a literal `%` in the Blob pathname, so `a%2Fb.md` and
`a/b.md` are two files.

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

export default defineRealtime({
  document: blobDocument(blob, { prefix: 'drafts/' }),
})
```

`save()` reads the stored file first. It throws `RealtimeDocumentConflict`
when the file changed, was created, or was deleted after the room loaded it.

> [!WARNING]
> Blob has no conditional write. An outside write between that read and the
> save's write is lost.

After you change the file outside the room, call `refreshRealtimeDocument()`
so the room loads it.

## Editor codecs

The editor codec owns the schema and the Markdown conversion. The server
builds the ProseMirror schema from `extensions()` and calls the codec. It never
creates an editor view, so a codec must not depend on the DOM. All functions
can be async.

```ts [shared/editor.ts] fragment
import { defineRealtimeEditor } from 'vite-hub/realtime'

export const pagesEditor = defineRealtimeEditor({
  extensions: () => [/* the same TipTap extensions the browser uses */],
  async fromMarkdown(markdown) { return await parseToProseMirrorJSON(markdown) },
  async toMarkdown(document) { return await serializeProseMirrorJSON(document) },
})
```

Use it in the definition with `editor: pagesEditor` and in the browser with
`useRealtimeTiptap('pages', id, { editor: pagesEditor })`. Both sides must
use the same extensions. When documents of one definition use different
schemas, `editor` can be a function of the document context, such as
`({ documentId }) => editorFor(documentId)`. Content uses this for one codec
per collection ([Edit Content together](https://vitehub.dev/docs/content/editing-together#live-editing)).
Without `editor`, Realtime uses `realtimeMarkdownEditor`: StarterKit, images,
tables, and raw YAML frontmatter as an atom node, parsed by `@tiptap/markdown`.

A lossless codec gives `toMarkdown(fromMarkdown(x)) === x` for your users'
documents. Realtime converts only at seed and save time, so a lossy codec
changes stored formatting on the first save, not on every keystroke.

### Guard documents the codec cannot keep

Most codecs normalize some Markdown and lose some. For example, a codec can
drop an undeclared `::callout` wrapper or unwrap raw HTML. A room saves on its
own, so the first save after any edit rewrites the stored document. Add
`verify()` to tell Realtime what the codec does with a stored document:

```ts [shared/editor.ts] fragment
export const pagesEditor = defineRealtimeEditor({
  extensions: () => [/* ... */],
  fromMarkdown,
  toMarkdown,
  async verify(markdown, { fields }) {
    const lost = await findUnsupportedConstructs(markdown)
    if (lost.length) return { writable: false, issues: lost.map(name => ({ message: `Unsupported: ${name}` })) }
    return { writable: true, canonical: await toMarkdown(await fromMarkdown(markdown)) }
  },
})
```

| Result | Effect |
| --- | --- |
| `writable: false` | The whole room opens read-only, body and fields. `status.error` explains why, and `status.issues` holds the `issues`. The room never saves. `save()` and `history.checkpoint()` answer `409` with `REALTIME_READ_ONLY`. A client update closes the connection with `4403`. |
| `canonical` | The body the codec writes for the unchanged document. A save with this body and unchanged fields writes nothing, so editing back to the start does not rewrite `* item` as `- item`. |
| `issues` | Shown in `status.issues` until the first save. |

Realtime calls `verify(body, { fields })` each time it seeds a room from the
stored document: when the room opens and after a re-seed. After you fix the
stored document, `refresh()` re-seeds the room, so it can become writable.

Realtime also guards every save, with or without `verify()`:

- It parses the Markdown it is about to save. If `fromMarkdown(toMarkdown(doc))`
  differs from the room body, it saves anyway and adds an entry to
  `status.issues`.
- If the stored body ends with a newline and the codec output does not,
  Realtime adds it back.

## Structured documents

Every room document has three root types, named in `realtimeDocumentLayout`:

| Root | Type | Written by |
| --- | --- | --- |
| `body` | `Y.XmlFragment` | Clients, through TipTap Collaboration. |
| `fields` | `Y.Map` with one JSON value per field | Clients. For two writes to the same field at once, the last write wins. Different fields never conflict. |
| `status` | `Y.Map`: `digest`, `error`, `issues`, `savedAt`, `savedBy` | The server only. `error` also holds the read-only reason from `verify()`. |

```ts
import { useRealtimeTiptap } from 'vite-hub/realtime/vue'

const realtime = useRealtimeTiptap('pages', 'about')

realtime.fields.value?.set('title', 'About us')
realtime.saveStatus.value // { digest, error, issues, savedAt, savedBy }
realtime.readOnly.value // true when the server rejects edits from this connection
realtime.readOnlyReason.value // 'access', 'document', or null
```

`readOnlyReason` is `access` when `authorize` denied write, and `document`
when `verify()` returned `writable: false`. Pass `editable: !readOnly` to the
editor, and show `saveStatus.error` for the `document` reason.

`status` lives in the Yjs document, so it arrives in order with its content,
survives a Durable Object restart, and plain Yjs can read it.
The server checks each client update against a copy of the room, and closes
the connection with `4403` when an update changes `status`. Write whole values
to a field. Nested objects in a field are replaced, not merged.

## Identity and authorization

```ts [server/realtime/pages.ts] fragment
export default defineRealtime({
  auth: true,
  identify: async ({ event }) => {
    const session = await getConsoleSession(event)
    return session && { id: session.user.id, name: session.user.name, image: session.user.image }
  },
  authorize: ({ user, documentId, mode }) => mode === 'read' || canEdit(user, documentId),
  document: pagesDocument,
})
```

| Option | Behaviour |
| --- | --- |
| `identify({ request, event, definition })` | Finds the user for each WebSocket and HTTP request. Return `null` for anonymous. Replaces the ViteHub Auth session. Requests with an `Origin` other than the app origin get 403. Behind a TLS-terminating proxy, the app origin uses the first `X-Forwarded-Proto` and `X-Forwarded-Host` values. |
| `auth: true` | Requires an identified user (else 401). Without `identify`, the user is the ViteHub Auth session, and the Auth trusted-origin policy applies. |
| `authorize({ request, event, user, documentId, mode })` | Called with `mode: 'read'` on every request, and `mode: 'write'` on connections, saves, and checkpoints. Return `false` to deny. A connection denied write is read-only: the server closes it with `4403` if it sends a change. `documentId` is `null` for the Workspace event channel. |

`event` is the request event of the WebSocket upgrade or HTTP request, a `RealtimeRequestEvent`. Read the request and its cookies from `event['~standard'].req`, the platform bindings from `event['~standard'].runtime`, and the H3 event context from `event.context`. See [Standard Context](https://vitehub.dev/docs/reference/runtime-context#standard-context).

An identified user replaces the client's `user` presence field, so nobody can
fake names and avatars. Without an identity, the client sets its own presence.

In the browser, pass `identity` to `useRealtimeTiptap` when presence comes
from a session other than ViteHub Auth. Server identity still wins.

## Carets

`realtime.extensions` includes other people's carets and selections, in each
person's presence color. They use TipTap's CollaborationCaret classes:
`collaboration-carets__caret`, `collaboration-carets__label`, and
`ProseMirror-yjs-selection`. TipTap ships no styles for them. `@vite-hub/ui`
styles them in `styles.css`.

Do not add `@tiptap/extension-collaboration-caret` next to Realtime. It has its
own copy of y-tiptap, so it cannot find Realtime's sync plugin and draws
nothing. To change the markup, pass `render` and `selectionRender` in `carets`,
with the same arguments as in TipTap's CollaborationCaret. `render` builds the
caret element. `selectionRender` returns a selection's decoration attributes.
Options you leave out keep the default.

```ts fragment
const realtime = useRealtimeTiptap('docs', 'guides/welcome.md', {
  carets: {
    render: (user) => {
      const caret = document.createElement('span')
      caret.className = 'caret'
      caret.style.borderColor = user.color
      caret.title = user.name
      return caret
    },
  },
})
const editor = useEditor({ extensions: realtime.extensions.value })
```

`realtime.extensions` binds the carets to the current document. It is empty
during SSR and while the document id is empty. It changes with the document,
so create the editor again for each document. Pass `carets: false` to leave
carets out.

## Presence without an editor

`connectRealtimeRoom(definition, documentId, options?)` joins one room to share presence, such as who is on a page and where each cursor is. It does not edit the room document and needs no Vue. [Comments](https://vitehub.dev/docs/comments/server-api#presence-rooms) uses it for its page rooms.

```ts fragment
import { connectRealtimeRoom } from '@vite-hub/realtime/client'
import { commentRoom, normalizeCommentPage } from '@vite-hub/comments'

const page = normalizeCommentPage(location.pathname)
const room = connectRealtimeRoom('comments', commentRoom(page), { changes: true, params: { page } })
room.setLocal('cursor', { x: 120, y: 80 })
room.onUpdate(() => render(room.people(), room.states()))
room.onChange(change => reload(change))
```

| Member | Behavior |
| --- | --- |
| `people()` | The people in the room, this browser included. |
| `states()` | The fields that each other client set with `setLocal()`, by client id. |
| `setLocal(field, value)` | Sets one field of this browser's awareness state. `null` removes it. |
| `status()` | `connecting`, `connected`, or `disconnected`. |
| `notify(change)`, `onChange(listener)` | Send and receive change hints through the workspace events room. Both need `changes: true`. |
| `onUpdate(listener)` | Runs when people, their fields, or the status change. Returns a function that removes it. |
| `destroy()` | Leaves the room. |

`identity` sets who this browser shows as. A definition with `identify` or `auth: true` replaces it with the server's identity. When the room re-seeds, the client joins the new seed on its own.

`params` sends query parameters on presence and workspace connections, so a definition can check context such as the current page.

> [!WARNING]
> The definition must check that context against the requested document before it grants access.

## Saves, refresh, and resets

| Client call | Endpoint | Result |
| --- | --- | --- |
| Idle save | none | 2 s after the last edit, at most 10 s, and when the last client leaves ([Configure](https://vitehub.dev/docs/realtime/configure#control-saves)). |
| `realtime.save()` | `POST …?document=save` | Saves now, including every edit this client sent. Returns `[error, saved]`, where `saved` is `{ digest, document, issues, savedAt, savedBy, error }`. |
| `realtime.refresh()` | `POST …?document=refresh` | Reloads the stored digest. If it changed, re-seeds the room and resets clients. Returns `[error, { digest, reseeded }]`. |
| `realtime.history.checkpoint()` | `POST …?history=checkpoint` | Saves, then records a history entry through the adapter, in one room operation. Returns `[error, checkpoint]`: the save plus `snapshot`. |

Each call returns `[error, data]` and does not throw. `error.code` is a `RealtimeRequestErrorCode` ([Realtime limits and errors](https://vitehub.dev/docs/realtime/limits-and-errors)).

```ts fragment
const [error, saved] = await realtime.save()
if (error) console.warn(error.code, error.why)
else console.log(saved.digest)
```

Save and checkpoint requests send the client's Yjs state. The server answers
`409` with `REALTIME_SYNC_PENDING` until it has that state, and the composable
retries. If a save or checkpoint finds the stored document changed, it
re-seeds the room and answers `409` with `REALTIME_SOURCE_CHANGED`.

When server code changes a stored document, for example in a route that an
Agent calls, call `refreshRealtimeDocument(definition, documentId)` after the
write. The open room re-seeds and resets its clients at once, so people see
the change without a browser `refresh()`:

```ts [server/api/pages/[id].put.ts] fragment
import { refreshRealtimeDocument } from 'vite-hub/realtime/server'

await db.pages.update(id, { body })
await refreshRealtimeDocument('pages', id)
```

It resolves `true` when a room re-seeded, and never throws. A room with
unsaved edits finds the change at its next save. A room that is not open loads
the new copy when it opens. On Cloudflare it reaches rooms through the Worker's
`$DurableObject` binding. Pass `{ durableObjects }` only to use another one.
Content calls it after every save from outside a live room.

Listen for resets to tell people what happened and recover unsaved text:

```ts fragment
realtime.onReset(({ reason, previous }) => {
  // reason: 'source-changed' (someone saved outside the room) or 'stale' (the room was re-seeded while offline)
  // previous: the Y.Doc this client held, destroyed after listeners return
})
```

After a reset, `realtime.document` and `realtime.extensions` point to a new
document. Create the editor again when `document` changes, as for a document
switch.

## Workspace documents and history

With `document: { workspace }`, saves write the Markdown file to the Workspace
working tree with `ifDigest`, and keep its media type and metadata. Workspace
hooks can normalize the file. Realtime merges the stored form back into the
room. Saves need a Workspace Store with conditional writes. A Cloudflare room
also needs a durable Workspace Store.

`history.checkpoint()` commits the working tree to Workspace history. The
commit includes every staged path, also other documents saved by their rooms.
If Workspace history moved, Realtime rebases onto the remote head and retries.
A path changed both locally and remotely takes the remote version and re-seeds
the room.

Turning the composable off destroys its document provider, disconnects
Workspace events, clears queued notifications, and makes requests return
`REALTIME_DISABLED`. Turning it on reconnects both providers for the current
document.

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

- [Overview](https://vitehub.dev/raw/docs/realtime.md)
- [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) (this page)
- [Limits and errors](https://vitehub.dev/raw/docs/realtime/limits-and-errors.md)

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

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