Back to snippets
yjs_open_collaboration_provider_quickstart_with_text_sync.ts
typescriptInitializes a Yjs Doc and connects it to the Open Collaboration s
Agent Votes
0
1
0% positive
yjs_open_collaboration_provider_quickstart_with_text_sync.ts
1import * as Y from 'yjs';
2import { OpenCollaborationProvider } from '@doyoubi/open-collaboration-yjs';
3
4// 1. Create a new Yjs document
5const ydoc = new Y.Doc();
6
7// 2. Initialize the Open Collaboration Provider
8// Replace 'your-room-name' with a unique identifier for your session
9// Replace 'your-app-id' with your actual application ID from the service
10const provider = new OpenCollaborationProvider({
11 doc: ydoc,
12 roomName: 'quickstart-room',
13 appId: 'your-app-id',
14 // Optional: Pass an auth token if your backend requires it
15 // token: 'your-auth-token'
16});
17
18// 3. (Optional) Observe changes on a Y.Text type
19const ytext = ydoc.getText('codemirror');
20ytext.observe(event => {
21 console.log('Document updated:', ytext.toString());
22});
23
24// 4. Handle connection status
25provider.on('status', ({ status }: { status: string }) => {
26 console.log(`Connection status: ${status}`); // 'connected' or 'disconnected'
27});
28
29// 5. Cleanup when done
30// provider.destroy();