Module boundaries and relationships
Understand the responsibilities, data flow, and assembly of the Collaboration Client, Transport, Endpoint, Service, and Database Adapter.
Univer Collaboration consists of a browser Client and a set of server-side modules. Each module owns one class of responsibility. Applications compose the modules through public interfaces, so their code does not depend on the internals of the OT1 implementation, the protocol, or the persistence layer.
Complete module diagram
Collaboration Client
The browser Collaboration Client runs alongside the Web SDK and handles the client side of collaboration:
- It requests Unit snapshots and any confirmed changesets that it is missing.
- It uses a one-time Session Ticket to open a WebSocket connection.
- It joins Unit Rooms and exchanges Presence with other members.
- It submits local mutations to the server as changesets.
- It receives ACKs and broadcasts, and after a reconnection it fetches the changes it missed.
The Client does not determine whether a user is trusted, and it is not the authoritative data source. That decision belongs to the server, which enforces identity and authorization in the corresponding Middleware.
Node Transport
@univerjs-pro/collaboration-transport-node is the Node.js entry point for HTTP and WebSocket traffic. It receives requests and WebSocket upgrades from the host HTTP server, runs the entry Middleware registered by the application, and routes traffic to Endpoints.
The Transport does not understand OT, Unit data, or ACL models. Assembly uses three methods:
transport.use(httpMiddleware);transport.useUpgrade(upgradeMiddleware);transport.register(endpoint);use()handles ordinary HTTP requests and can establishuserID,customData, logs, or traces.useUpgrade()handles the WebSocket handshake and can reject the connection before it is established.register()registers a protocol Endpoint, whose lifecycle the Transport then manages.
Collaboration Endpoint
@univerjs-pro/collaboration-endpoint implements the protocol used by the Univer Collaboration Client and owns the following capabilities:
| Capability | Endpoint responsibility |
|---|---|
| Content loading | Returns Unit snapshots and blocks, along with any confirmed changesets that the Client lacks |
| Change submission | Receives changesets, returns ACKs, and broadcasts confirmed changesets to the Room |
| Session connection | Issues one-time Session Tickets, then establishes and maintains WebSocket Sessions |
| Room collaboration | Handles JOIN, LEAVE, member state, and Presence |
| Unit lifecycle | Maps delete and recovery requests to the corresponding Service calls |
| Multi-node | Relays Rooms, Presence, and broadcasts across nodes through an optional pub/sub adapter and a shared ticket store |
The Endpoint does not perform OT and does not store collaboration data itself. It obtains the authoritative result from the Service and maps that result back to the Client protocol. Product operations such as creating a document belong in an application API that calls the Service.
Collaboration Service
@univerjs-pro/collaboration-service is the collaboration core and does not depend on the network. It covers Sheet, Doc, Slide, Board, Base, and PDF:
- It reads Unit recovery data and confirmed changesets.
- It creates Units from Unit Data or Snapshots.
- Submitting a changeset runs OT and advances the continuous revision2.
- It identifies duplicate submissions through
(unitID, sid, reqId). - It soft-deletes, recovers, or permanently deletes Units.
- It runs Service Middleware and emits Events after confirmation.
The Service has no HTTP or WebSocket layer, and it does not know about users, roles, ACLs, hierarchy, or file management. The Endpoint builds the browser protocol on top of the Service. Applications can also call the Service's public API directly from business APIs or background tasks.
Core APIs
| Capability | Service API |
|---|---|
| Load a Unit | getUnitLoadData(), getUnitLoadDataWithBlocks() |
| Read incremental data | getChangesets(), getSheetBlock() |
| Create a Unit | createUnitFromData(), createUnitFromSnapshot() |
| Submit changes | submitChangeset() |
| Delete and recover | deleteUnits(), recoverUnits() |
Database Adapter
The Database Adapter is the contract between the Service and storage. It must guarantee:
- atomic creation of an initial snapshot and its dependencies;
- revision compare-and-swap;
- idempotent changeset submission;
- snapshot visibility only after all of its dependencies exist;
- all-or-nothing Unit lifecycle batches.
Applications connect storage through IDatabaseAdapter. For help choosing a built-in adapter or implementing a custom one, see Database Adapters.
Four core concepts
| Concept | Meaning |
|---|---|
| Unit | Independently loadable and collaborative Univer content, with a unique unitID and a type |
| Snapshot | Unit content persisted at a revision, not the live mutable object |
| Changeset | A batch of mutations submitted together, with a base revision and an identity for idempotency |
| Revision | The continuous version number of confirmed Unit state, starting at 1 |
All changes to running content go through Facade APIs or Commands. Mutating a snapshot object does not update the live Unit in the Client or the Service.
Loading a Unit
The userID/customData established by Transport Middleware travels with the current HTTP request into Service Middleware. The application decides whether a read is allowed. A client does not need to join a Room before it reads over plain HTTP.
Submitting an edit
Facade / Command → Mutation → Client Changeset → Endpoint submit → Service Middleware → OT + Revision CAS → Adapter atomic commit → confirmed Changeset → ACK / Room broadcastThe confirmed revision in the Database Adapter is the authoritative state. ACKs and broadcasts keep online clients synchronized with low latency. A client that misses a message can fetch the confirmed changesets it lacks using the revision it already knows.
Minimal server assembly
import { createServer } from "node:http";import { MemoryDatabaseAdapter } from "@univerjs-pro/collaboration-database-memory";import { UniverCollabEndpoint } from "@univerjs-pro/collaboration-endpoint";import { UniverCollabService } from "@univerjs-pro/collaboration-service";import { createNodeTransport } from "@univerjs-pro/collaboration-transport-node";const database = new MemoryDatabaseAdapter();const service = new UniverCollabService({ dbAdapter: database });const endpoint = new UniverCollabEndpoint(service);const transport = createNodeTransport();transport.use(async (context, next) => { context.userID = "demo-user"; await next();});transport.register(endpoint);const server = createServer();transport.attach(server);server.listen(3010);The hard-coded user and the Memory Adapter demonstrate module assembly only. Real applications establish Context through Middleware and select an Adapter that matches their data requirements.
Create a Unit through an application API
Applications can define their own HTTP or RPC endpoints and call the service created above after authentication, input validation, and business checks pass:
Business client → POST /api/units → application authentication and validation → service.createUnitFromData() → Database AdapterThe Express example below assumes that authentication Middleware has set response.locals.user and that request.body.data has already been validated as Workbook Data:
import { randomUUID } from "node:crypto";import { json } from "express";import { UniverType } from "@univerjs/protocol";app.post("/api/units", json({ limit: "1mb" }), async (request, response, next) => { try { const user = response.locals.user as { readonly userID: string }; const unitID = randomUUID(); const result = await service.createUnitFromData( { type: UniverType.UNIVER_SHEET, data: { ...request.body.data, id: unitID, rev: 1, }, }, { userID: user.userID, customData: { traceID: randomUUID() }, }, ); response.status(result.status === "created" ? 201 : 200).json(result); } catch (error) { next(error); }});POST /api/units is an application route and is not part of the Collaboration Client protocol. Calling createUnitFromData() still runs the Service's createUnit Middleware. Product records, owner ACLs, error-response mapping, and failure compensation remain the responsibility of the application.
Resource cleanup
transport.dispose() disposes the Endpoints registered with register(). The Service and the Database Adapter that the application injected must be disposed separately, in the reverse of the assembly order:
await transport.dispose();await service.dispose();await database.dispose();If the application supplied a pub/sub adapter, it must also dispose that adapter and its connections after the Transport. The Endpoint does not dispose them.
To extend behavior at these module boundaries, continue with Middleware and Events.
Footnotes
-
Operational transformation (OT) adjusts operations created concurrently against different document revisions so they can be applied consistently. It operates on edits, rather than replacing the whole document with whichever snapshot arrives last. ↩
-
A revision identifies a position in a Unit’s confirmed edit history. A changeset contains the operations in a submission; its confirmation advances that history. A revision is not an SDK package version or a timestamp. ↩
How is this guide?