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.

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";

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

// Listen …
server.listen();

// Destroy …
server.destroy();

You can access the instance of hocuspocus through the webserver to call it's methods.

// …

server.hocuspocus.getDocumentsCount();

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.
import { Server } from "@hocuspocus/server";

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

// Listen …
server.listen();

// Destroy …
server.destroy();

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.