Hocuspocus room server guide

Set up a Hocuspocus room server, move an existing server to the collaboration extension, and upgrade rooms when the collaboration format changes.

collaborationServerExtension does the room's part of a Hocuspocus server. Add it to extensions, and write two things: an access check and a storage adapter.

You writeThe extension does
onAuthenticate: who can open which roomRefuses clients of another collaboration format
storage.load and storage.save: where room bytes areChecks each stored room before it becomes the live room
storage.backUp, optionalStores each room with a DOCX export
createRoom, optionalUpgrades a stored room when the collaboration format changes

For every option and limit, see Run a room server.

Quickstart

1. Install

The Hocuspocus v4 server runs on Node.js, not on Bun.

npm install @docx-editor.dev/pro yjs @hocuspocus/server

2. Add storage for three values

Each room has a state, a DOCX export, and the SHA-256 digest of the exported state. For example, in SQL:

create table rooms (
  name    text primary key,
  state   bytea not null,
  docx    bytea,          -- The export. Null until the first export.
  digest  text,           -- The digest that arrived with `docx`.
  previous bytea          -- The state before the last upgrade.
);

3. Write the server

import { Server } from '@hocuspocus/server';
import {
  collaborationServerExtension,
  type CollaborationRoomStorage,
} from '@docx-editor.dev/pro/collaboration';

// Your database client and your access check.
interface RoomRow {
  state: Uint8Array;
  docx: Uint8Array | null;
  digest: string | null;
}
declare const db: { query(sql: string, values: unknown[]): Promise<RoomRow[]> };
declare function verifyDocumentAccess(token: string, roomName: string): Promise<void>;

const storage: CollaborationRoomStorage = {
  async load(name) {
    const [room] = await db.query('select state, docx, digest from rooms where name = $1', [name]);
    if (!room) return null;
    const exported =
      room.docx && room.digest ? { docx: room.docx, stateDigest: room.digest } : null;
    return { state: room.state, export: exported };
  },

  async save(name, { state, export: exported }) {
    // `exported` is null when this store has no new export: keep the stored one.
    await db.query(
      `insert into rooms (name, state, docx, digest) values ($1, $2, $3, $4)
       on conflict (name) do update set
         state = excluded.state,
         docx = coalesce(excluded.docx, rooms.docx),
         digest = coalesce(excluded.digest, rooms.digest)`,
      [name, state, exported?.docx ?? null, exported?.stateDigest ?? null]
    );
  },

  async backUp(name, state) {
    await db.query('update rooms set previous = $2 where name = $1', [name, state]);
  },
};

const server = new Server({
  port: 1234,
  async onAuthenticate({ token, documentName }) {
    await verifyDocumentAccess(token, documentName);
  },
  extensions: [
    collaborationServerExtension({
      storage,
      onEvent: (event) => console.log(JSON.stringify(event)),
    }),
  ],
});
await server.listen();

Write the state and the export in one statement, as the sample does. If your storage cannot, write the state first.

4. Connect the client

The client needs no server-specific code. The hook sends the collaboration format to the server:

const { document, modules, session, error } = useHocuspocusCollaboration({
  room: {
    url: 'wss://collab.example.test',
    roomId,
    token,
    identity: { actorId, name },
    bootstrap: { kind: 'create-or-join', document: bytes },
  },
});

For the full component, see Connect to a Hocuspocus server.

Move an existing server to the extension

  1. Deploy clients of a release that has the extension. A server with the extension refuses clients that send no collaborationFormat parameter. If you cannot update all clients first, see Admit earlier clients during a rollout.
  2. Add the export and the digest to your room storage.
  3. Write load, save, and backUp as the quickstart shows.
  4. Remove your own room code, as the following table lists.
  5. Deploy the server on the same release and collaboration format as before.
Your codeAction
Format check in onAuthenticate, and the token envelope for the versionRemove. Keep your access check.
prepareCollaborationServerDocument in onLoadDocumentRemove.
Saved-room check and Y.applyUpdate in onLoadDocumentRemove. storage.load returns the bytes.
document.flush() in afterLoadDocumentRemove.
checkCollaborationRoomGeneration in beforeHandleMessageRemove.
onStoreDocumentRemove. storage.save receives the bytes.
createCollaborationState for a room that storage does not holdMove to the createRoom option.
compactCollaborationState on loadReplace with compactOnLoad: true.

Stored rooms need no conversion. A room gets its export when the server first loads or stores it. For rooms that nobody opens, run Export existing rooms once.

Deploy a release that changes the format

Most releases keep the collaboration format, and you deploy them as any other release. To know before you deploy, compare format for the two releases in the release data.

When the formats differ:

  1. Ask users with unsent offline edits to reconnect. Pending edits do not transfer to an upgraded room.
  2. Stop every room server of the earlier release.
  3. Deploy the clients and the room servers of the target release together.
  4. Watch onEvent for room-upgraded and room-upgrade-refused.

In Kubernetes, the Recreate strategy stops the earlier servers first:

spec:
  strategy:
    type: Recreate

You install no earlier release and run no migration job. The server does the following when the first participant opens a room:

Rendering diagram…

flowchart TD
  A[Participant opens a room] --> B{Format of the stored state}
  B -- This release --> C[Check the room, then load it]
  B -- Earlier release --> D{Export present, digest equal, state within the size limit}
  D -- No --> E[Refuse with saved-room-unavailable. The room stays unchanged.]
  D -- Yes --> F[Create the room again from the DOCX export]
  F --> G{New room equals the export}
  G -- No --> E
  G -- Yes --> H[Call backUp, store the upgraded room, load it]

The room keeps its ID and its links. Collaboration undo history starts afresh.

Monitor rooms

onEvent receives a typed object for each action. Events hold codes, part names, and positions, never document text.

import type { CollaborationServerEvent } from '@docx-editor.dev/pro/collaboration';

declare const metrics: { count(name: string, labels: Record<string, string>): void };

export function onEvent(event: CollaborationServerEvent): void {
  const reason = event.type === 'room-upgrade-refused' ? event.reason : '';
  metrics.count('collaboration_room_events', { type: event.type, reason });
  if (event.type !== 'room-upgraded') console.warn(JSON.stringify(event));
}
Event typeMeaningAction
room-upgradedThe server upgraded a stored room.None, when hidden is 0.
room-upgrade-refusedThe server did not upgrade a room of an earlier format.See the next table.
room-load-refusedThe stored room is of a later format, or it forms no document.Serve it with the release that wrote it, or restore it.
room-export-failedThe room does not export, so the server did not store it.Check code. Storage keeps the last room that exported.
room-compaction-failedA compaction on load failed. The room loaded as stored.Check code.

A refused upgrade leaves the room unchanged in storage. reason says why:

reasonCauseAction
no-exportStorage holds no export of the room.Migrate the room in a job.
stale-exportThe server stopped before it exported the last state.Migrate the room in a job.
too-largeThe state is larger than maxUpgradeStateBytes, 64 MiB by default.Migrate the room in a job, or raise the limit.
check-failedThe upgraded room differs from the export.Read changedParts in the event, and report the defect.
errorThe state or the export cannot be read.Act on code.
disabledupgradeOnLoad is false.Migrate the room in a job.

The job exports each room with the earlier release and migrates it with the target release. See Migrate rooms on a server.

Troubleshoot a refused client

The client reports the server's code in error.code or in the session status:

Code on the clientCauseFix
collaboration-format-mismatchThe client and the server are of different formats, or the client sent no format.Deploy the client and the server from one release. Reload the tab.
saved-room-unavailableThe server did not upgrade the stored room.Find room-upgrade-refused in the server log, and migrate the room.
invalid-saved-roomThe stored state forms no document.Restore the room from a backup or from its DOCX export.
room-generation-changedThe server upgraded or compacted the room while this tab was away.Rejoin the room.
initialization-timeoutThe server did not synchronize in time. A long upgrade can cause this.Retry, or raise syncedTimeoutMs.

Production checklist

  • Route each room to one server process. Two processes that load one earlier-format room each create a room.
  • Validate documentName in onAuthenticate. It is client input, and it reaches your storage.
  • Implement backUp, and keep the earlier state until you accept an upgrade.
  • Plan for blocking work. An export and an upgrade run on the server's thread: 0.2 and 0.5 seconds for a 75 KB document, 2 and 7 seconds for a 521-page document. For long documents, set exportIntervalMs. See Plan for the cost of exports and upgrades.
  • Compress stored state. A room state is about seven times its uncompressed XML, and gzip reduces it about nine times.
  • Alert on room-upgrade-refused and room-export-failed.

Try an upgrade

The Hocuspocus example uses the extension with file storage. Its server/simulate-format-change.ts script lets you watch an upgrade with one release:

node server/server.ts                  # Open a room, type, close the tab, then stop the server.
node server/simulate-format-change.ts  # Each room now names an earlier format.
node server/server.ts                  # Open the room again: "upgraded from ... to ...".

See also