@bcibibi/y-sync-client
v1.0.0
Published
Client for syncing Yjs documents over WebSockets connections. This package is part of the y-sync project.
Maintainers
Readme
@bcibibi/y-sync-client
Beta: this package is currently in beta and APIs/behavior may evolve.
Yjs client for synchronizing a document over WebSocket.
Installation
npm install @bcibibi/y-sync-client yjsQuick Usage
import { YSyncClient } from '@bcibibi/y-sync-client'
const client = new YSyncClient('ws://localhost:1234')
// Fetch (or create) a collaborative Y.Doc by id
const ydoc = await client.getYDocument('demo-doc')
// Access shared awareness state if needed
const awareness = client.getAwareness()
// Close the underlying WebSocket when done
client.close()In Node.js environments, pass a WebSocket implementation:
import WebSocket from 'ws'
import { YSyncClient } from '@bcibibi/y-sync-client'
const client = new YSyncClient('ws://localhost:1234', {
websocket: WebSocket
})Options
YSyncClient constructor signature:
new YSyncClient(url: string, options?: YSyncClientOptions)Available options:
autoconnect?: boolean(default:true)- Automatically opens the WebSocket connection when the client is created.
- Set to
falseto delay automatic connection behavior.
reconnectInterval?: number(default:5000)- Reconnect check interval in milliseconds.
- If no message is received within this interval, the client triggers a reconnect attempt.
websocket?: YSyncWebSocketConstructor- Custom WebSocket constructor.
- Required in environments where
globalThis.WebSocketis not available (typically Node.js).
Example with options:
import WebSocket from 'ws'
import { YSyncClient } from '@bcibibi/y-sync-client'
const client = new YSyncClient('ws://localhost:1234', {
autoconnect: true,
reconnectInterval: 3000,
websocket: WebSocket
})See also the React demo: ../react/README.md
