协同在 Node.js 环境下的使用示例

Node.js 可以作为协同客户端使用:从服务端加载已有的文档,通过 Facade API 编辑内容,并将更改同步到所有打开该文档的其他客户端。运行示例之前,需要准备协同服务、客户端许可证和一个可访问的文档 ID。

插件配置

UniverLicensePlugin

  • license
    string
    客户端许可证文件的内容。

UniverDrawingPlugin

3
  • override
    [DependencyIdentifier, Provider | null][]
    绘图插件使用的依赖覆盖项。
    • [0]
      DependencyIdentifier
      依赖的标识。图片存储用 IImageIoService。
    • [1]
      Provider | null
      替换默认实现的提供者。传入 null 表示由协同提供该服务。

UniverCollaborationClientPlugin

15
  • socketService
    Ctor<ICollaborationSocketService>
    当前运行环境使用的 Socket 实现。
  • enableOfflineEditing
    boolean默认值: false
    离线时允许在本地编辑。
  • enableSingleActiveInstanceLock
    boolean默认值: false
    同一文档只能在一个浏览器实例中处于编辑状态。
  • snapshotServerUrl
    string默认值: '/universer-api/snapshot'
    读取文档快照的接口地址。
  • collabSubmitChangesetUrl
    string默认值: '/universer-api/comb'
    提交变更集的接口地址。
  • collabWebSocketUrl
    string默认值: 'ws://127.0.0.1:8000/universer-api/comb/connect'
    同步实时变更的 WebSocket 地址。
  • wsSessionTicketUrl
    string默认值: '/universer-api/user/session-ticket'
    获取 WebSocket 会话票据的接口地址。
  • uploadFileServerUrl
    string默认值: '/universer-api/stream/file/upload'
    上传文件的接口地址。
  • signUrlServerUrl
    string默认值: '/universer-api/file/{fileID}/sign-url'
    签发文件 URL 的接口地址。保留 {fileID} 占位符。
  • downloadEndpointUrl
    string
    下载文件的基础地址。
  • authzUrl
    string默认值: '/universer-api/authz'
    检查权限的接口地址。
  • sendChangesetTimeout
    number默认值: 2000
    变更发出前批量合并的等待时长,单位为毫秒。
  • retryConnectingInterval
    number默认值: 20000
    断线后的重连间隔,单位为毫秒。
  • customHeaders
    Record<string, string>
    连接时额外发送的请求头。
    • [header]
      string
      这个 HTTP 请求头发送的值。

示例文件

文件结构如下:

text
src/docs/node/├── main.ts└── univer.ts
  • univer.ts:创建 Univer,注册文档功能插件、Facade 扩展和 Node.js 协同客户端。
  • main.ts:加载协同文档,依次演示段落、文本、页眉页脚、提示块、代码块、引用块、列表、形状、表格和分栏。

请替换 univer.ts 中的 your-license、服务地址和 Cookie,以及 main.ts 中的文档 ID。

univer.tsTypeScript
import { UniverCollaborationPlugin } from '@univerjs-pro/collaboration'import { UniverCollaborationClientPlugin } from '@univerjs-pro/collaboration-client'import {  NodeCollaborationSocketService,  UniverCollaborationClientNodePlugin,} from '@univerjs-pro/collaboration-client-node'import { UniverDocsCalloutPlugin } from '@univerjs-pro/docs-callout'import { UniverDocsCodePlugin } from '@univerjs-pro/docs-code'import { UniverDocsColumnPlugin } from '@univerjs-pro/docs-column'import { UniverDocsLatexPlugin } from '@univerjs-pro/docs-latex'import { UniverDocsListPlugin } from '@univerjs-pro/docs-list'import { UniverDocsQuotePlugin } from '@univerjs-pro/docs-quote'import { UniverDocsReferencePlugin } from '@univerjs-pro/docs-reference'import { UniverDocsShapePlugin } from '@univerjs-pro/docs-shape'import { UniverDocsTablePlugin } from '@univerjs-pro/docs-table'import { UniverLicensePlugin } from '@univerjs-pro/license'import {  IAuthzIoService,  IImageIoService,  IMentionIOService,  IUndoRedoService,  LocaleType,  Univer,} from '@univerjs/core'import { UniverDocsPlugin } from '@univerjs/docs'import { UniverDocsDrawingPlugin } from '@univerjs/docs-drawing'import { UniverDocsHyperLinkPlugin } from '@univerjs/docs-hyper-link'import { UniverDrawingPlugin } from '@univerjs/drawing'import { UniverRenderEnginePlugin } from '@univerjs/engine-render'import { UniverNetworkPlugin } from '@univerjs/network'import { UniverThreadCommentPlugin } from '@univerjs/thread-comment'import '@univerjs/docs/facade'import '@univerjs/docs-drawing/facade'import '@univerjs-pro/docs-shape/facade'import '@univerjs-pro/docs-callout/facade'import '@univerjs-pro/docs-reference/facade'import '@univerjs-pro/docs-code/facade'import '@univerjs-pro/docs-list/facade'import '@univerjs-pro/docs-quote/facade'import '@univerjs-pro/docs-table/facade'import '@univerjs-pro/docs-column/facade'import '@univerjs-pro/docs-latex/facade'import '@univerjs-pro/collaboration-client/facade'export function createUniverOnNode(): Univer {  const univer = new Univer({    locale: LocaleType.ZH_CN,    locales: {},    override: [      [IAuthzIoService, null],      [IUndoRedoService, null],      [IMentionIOService, null],    ],  })  univer.registerPlugin(UniverLicensePlugin, {    license: 'your-license',  })  univer.registerPlugin(UniverRenderEnginePlugin)  univer.registerPlugin(UniverThreadCommentPlugin)  univer.registerPlugin(UniverDrawingPlugin, {    override: [[IImageIoService, null]],  })  univer.registerPlugin(UniverDocsPlugin)  univer.registerPlugin(UniverDocsDrawingPlugin)  univer.registerPlugin(UniverDocsHyperLinkPlugin)  univer.registerPlugin(UniverDocsReferencePlugin)  univer.registerPlugin(UniverDocsCalloutPlugin)  univer.registerPlugin(UniverDocsCodePlugin)  univer.registerPlugin(UniverDocsListPlugin)  univer.registerPlugin(UniverDocsQuotePlugin)  univer.registerPlugin(UniverDocsShapePlugin)  univer.registerPlugin(UniverDocsTablePlugin)  univer.registerPlugin(UniverDocsColumnPlugin)  univer.registerPlugin(UniverDocsLatexPlugin)  registerCollaborationPlugins(univer)  return univer}function registerCollaborationPlugins(univer: Univer): void {  const serverUrl = 'https://your-univer-server.example.com'  univer.registerPlugin(UniverNetworkPlugin)  univer.registerPlugin(UniverCollaborationPlugin)  univer.registerPlugin(UniverCollaborationClientPlugin, {    socketService: NodeCollaborationSocketService,    enableOfflineEditing: false,    enableSingleActiveInstanceLock: false,    snapshotServerUrl: `${serverUrl}/universer-api/snapshot`,    collabSubmitChangesetUrl: `${serverUrl}/universer-api/comb`,    collabWebSocketUrl: `${serverUrl}/universer-api/comb/connect`,    wsSessionTicketUrl: `${serverUrl}/universer-api/user/session-ticket`,    uploadFileServerUrl: `${serverUrl}/universer-api/stream/file/upload`,    signUrlServerUrl: `${serverUrl}/universer-api/file/{fileID}/sign-url`,    downloadEndpointUrl: serverUrl,    authzUrl: `${serverUrl}/universer-api/authz`,    sendChangesetTimeout: 200,    retryConnectingInterval: 1000,    customHeaders: {      // 示例:如果 Univer 服务端使用 Cookie 认证,可以在这里设置 'cookie' 请求头。      cookie: 'your-cookie',      // ……其他请求头    },  })  univer.registerPlugin(UniverCollaborationClientNodePlugin)}

运行说明

loadDocAsync() 加载服务端已有的文档,因此不需要调用 createDocument()。协同服务通过 customHeaders 中的 Cookie 等认证信息识别用户并检查权限。连接的 memberID 由服务端返回。

示例关闭了浏览器离线缓存和多标签页实例锁,因此不需要为账号之间的缓存隔离调用 setCurrentUser()。如果之后使用评论等依赖本地当前用户的功能,可以通过 UserManagerService 设置业务用户,但这不会替代服务端认证。

页眉页脚示例仅适用于分页文档,分栏示例仅适用于现代文档。示例通过 isModern() 判断文档模式,不适用的操作会被跳过。

示例会向已有文档写入内容,因此请使用测试文档运行。示例结束后协同连接不会断开,可在浏览器中打开同一份文档查看更改。flush() 会等待服务端确认已排队的协同修改。如需改为一次性脚本,请在所有异步操作和同步完成后调用 univer.dispose() 释放实例。

你觉得这篇文档如何?

© 2026 DreamNum Co., Ltd.