AI agents: fetch the documentation index at llms.txt. Markdown versions are available by appending .md to any page URL, including this page's markdown.
YjsDocumentManager
Manages Y.Doc instances, sync protocol, connection state, and content change tracking for collaborative editing.
Creates and caches Y.Doc instances by DocumentKey, handles the Yjs sync protocol, tracks connection state, and notifies on content changes.
Creating an instance
Provide a clientId for attribution and a connId for echo suppression.
import { YjsDocumentManager } from "@stratasync/y-doc";import type { YjsDocumentManagerConfig } from "@stratasync/y-doc";const config: YjsDocumentManagerConfig = { clientId: "client-abc-123", connId: "conn-xyz-456",};const documentManager = new YjsDocumentManager(config);
YjsDocumentManagerConfig
Property
Type
Description
clientId
string
Logical client identity for attribution. Stable across tabs and sessions.
connId
string
Connection-scoped identity for echo suppression of yjs_update messages.
Setting the transport
Set a transport before connecting to any documents.
Sends sync messages and receives updates and errors.
API reference
getDocument
Returns the Y.Doc for a key, creating one if needed.
import type { DocumentKey } from "@stratasync/y-doc";import type * as Y from "yjs";const docKey: DocumentKey = { entityType: "Task", entityId: "task-123", fieldName: "description",};const doc: Y.Doc = documentManager.getDocument(docKey);const fragment = doc.getXmlFragment("prosemirror");
Signature:getDocument(docKey: DocumentKey): Y.Doc
Cached per key: subsequent calls return the same instance. Each document uses a shared ProseMirror fragment named "prosemirror".
connect
Connects to a document and initiates the Yjs sync protocol.
documentManager.connect(docKey);// With initial contentdocumentManager.connect(docKey, { initialContent: "Default description text",});