Usage

Usage

There are two ways on how you can use hocuspocus. Either with the built-in server. Or with another framework, for example with Express.

Since v4 Hocuspocus supports multiple runtimes: Node.js (with ws or uWebSockets.js), Bun, Deno, and Cloudflare Workers. The built-in Server class works as before for Node.js; for other runtimes use Hocuspocus directly and pass any WebSocketLike instance to handleConnection(). Node.js 22+ is required.

Hocuspocus Server

Using the built-in server make sure to import Server from @hocuspocus/server. You configure the server as described under configuration. The built-in server spins up a webserver and a websocket server.

import { Server } from "@hocuspocus/server";

const server = new Server({
  port: 1234,
});

server.listen();

You can access the instance of hocuspocus through the webserver to call its methods.

// …

server.hocuspocus.getDocumentsCount();

Stopping the server

Call server.destroy() to shut the server down cleanly — for example from a SIGINT / SIGTERM handler, in tests, or when hot-reloading during development.

server.destroy();

Hocuspocus

As mentioned earlier, you can use hocuspocus without the built-in server. Make sure to import Hocuspocus from the @hocuspocus/server package.

import { Hocuspocus } from "@hocuspocus/server";

// Configure hocuspocus
const hocuspocus = new Hocuspocus({
  name: "hocuspocus-fra1-01",
})

// …

Check out the examples to learn more.

Methods

Server

MethodDescription
listen(port, callback)Start the server.
destroy()Stop the server.

Hocuspocus

MethodDescription
configure(configuration)Pass custom settings.
handleConnection(incoming, request, context)Bind the server to an existing server instance.
getDocumentsCount()Get the total number of active documents
getConnectionsCount()Get the total number of active connections
closeConnections(documentName?)Close all connections, or to a specific document.
openDirectConnection(documentName, context)Creates a local connection to a document.