Collaboration
With collaboration enabled, several users can edit the same board and see each other's changes. Follow collaboration integration to set up the backend, authentication, permissions, and storage, then configure the license.
Configure and load a document
Use the Collaboration Quick Start to add Collaboration Client configuration to an existing Boards editor. That example uses Sheets, so the backend must still create and persist a Boards document and return its ID to the browser.
Set the current user before loading the document. Collaboration integration describes the procedure.
import '@univerjs-pro/boards/facade'import { UserManagerService } from '@univerjs/core'import '@univerjs-pro/collaboration-client/facade'univer.__getInjector().get(UserManagerService).setCurrentUser({ userID, name, avatar })const document = await univerAPI.getCollaboration().loadBoardAsync('your-document-id')if (!document) throw new Error('Document could not be loaded')Reopening or sharing the document uses the same ID. Do not create a local blank document in place of the server document.
For saving indicators and for protecting edits that the server has not yet acknowledged before a reload, see synchronization status, retries, and conflict recovery.
Removal of built-in URL loading for collaborative documents
Note
Starting with version 1.2.0, Univer no longer loads collaborative documents from the URL. It no longer reads the unit and type URL parameters automatically.
If the application depends on that behavior, a custom plugin can restore it.
Integrate the custom plugin
Copy the four files below into the project and keep the following directory structure:
custom-url-load/├── plugin.ts├── controllers/│ └── data-loader.controller.ts└── services/ ├── url.service.ts └── web-url.service.tsInitialize the editor and collaboration plugins, set the current user, and then register CustomUrlLoadPlugin.
import { UserManagerService } from '@univerjs/core'import { CustomUrlLoadPlugin } from './custom-url-load/plugin'univer.__getInjector().get(UserManagerService).setCurrentUser({ userID, name, avatar })univer.registerPlugin(CustomUrlLoadPlugin)Once registered, the plugin handles loading, so loadBoardAsync() is not required for the same document. If the application already loads documents through the Facade, keep the approach from the previous section and handle URLs according to the application's requirements.
Complete plugin code
import { UniverCollaborationClientPlugin } from '@univerjs-pro/collaboration-client'import { DependentOn, Inject, Injector, Plugin } from '@univerjs/core'import { CollaborationDataLoaderController } from './controllers/data-loader.controller'import { IURLService } from './services/url.service'import { WebURLService } from './services/web-url.service'@DependentOn(UniverCollaborationClientPlugin)export class CustomUrlLoadPlugin extends Plugin { static override pluginName = 'CUSTOM_URL_LOAD_PLUGIN' constructor( _config: undefined, @Inject(Injector) protected override readonly _injector: Injector ) { super() } override onStarting(): void { this._injector.add([IURLService, { useClass: WebURLService }]) this._injector.add([CollaborationDataLoaderController]) this.disposeWithMe(this._injector.get(IURLService)) this.disposeWithMe(this._injector.get(CollaborationDataLoaderController)) }}import { DataLoaderService } from '@univerjs-pro/collaboration-client'import { ILogService, Inject, RxDisposable } from '@univerjs/core'import { IURLService } from '../services/url.service'/** * Load a collaborative document from URL parameters. */export class CollaborationDataLoaderController extends RxDisposable { constructor( @IURLService private readonly _urlService: IURLService, @ILogService private readonly _logService: ILogService, @Inject(DataLoaderService) private readonly _dataLoaderService: DataLoaderService ) { super() const unitId = this._urlService.getParam('unit') const type = this._urlService.getParam('type') if (!unitId || !type) { return } this._dataLoaderService.loadUnit(unitId, Number(type)) .catch((error: unknown) => { this._logService.error('[CollaborationDataLoaderController]', error) }) }}import type { IDisposable } from '@univerjs/core'import type { Observable } from 'rxjs'import { createIdentifier } from '@univerjs/core'export interface IURLService extends IDisposable { getParam: (key: string) => string | undefined setParam: (key: string, value: string, useReplace?: boolean) => void removeParam: (key: string, useReplace?: boolean) => void readonly urlChange$: Observable<void>}export const IURLService = createIdentifier<IURLService>('demo.custom-url-load.url-service')import type { IURLService } from './url.service'import { RxDisposable } from '@univerjs/core'import { fromEvent, map, shareReplay, takeUntil } from 'rxjs'export class WebURLService extends RxDisposable implements IURLService { readonly urlChange$ = fromEvent(window, 'popstate').pipe( map(() => undefined), takeUntil(this.dispose$), shareReplay({ bufferSize: 1, refCount: true }) ) getParam(key: string): string | undefined { return new URL(window.location.href).searchParams.get(key) ?? undefined } setParam(key: string, value: string, useReplace = false): void { const url = new URL(window.location.href) url.searchParams.set(key, value) this._updateUrl(url, useReplace) } removeParam(key: string, useReplace = false): void { const url = new URL(window.location.href) url.searchParams.delete(key) this._updateUrl(url, useReplace) } private _updateUrl(url: URL, useReplace: boolean): void { if (useReplace) { window.history.replaceState(window.history.state, '', url.toString()) } else { window.history.pushState(window.history.state, '', url.toString()) } }}Verify synchronization and permissions
Open the same ID in two separate browser sessions. Edit text or move an element in one session, then check the other. Confirm that content survives page reloads, reconnections, and a service restart. Use viewer and unauthorized accounts to confirm that the server rejects writes and reads that their permissions forbid.
Collaboration does not enable Edit history or Import and export; each requires separate configuration.
How is this guide?