TLSyncClient
See source codeMain client-side synchronization engine for collaborative tldraw applications.
TLSyncClient manages bidirectional synchronization between a local tldraw Store and a remote sync server. It uses an optimistic update model where local changes are immediately applied for responsive UI, then sent to the server for validation and distribution to other clients.
The synchronization follows a git-like push/pull/rebase model:
- Push: Local changes are sent to server as diff operations
- Pull: Server changes are received and applied locally
- Rebase: Conflicting changes are resolved by undoing local changes, applying server changes, then re-applying local changes on top
class TLSyncClient<R extends UnknownRecord, S extends Store<R> = Store<R>> {}
Example
import { createTLStore } from '@tldraw/store'
import { ClientWebSocketAdapter, TLSyncClient } from '@tldraw/sync-core'
// Create store and socket
const store = createTLStore({ schema: mySchema })
const socket = new ClientWebSocketAdapter('ws://localhost:3000/sync')
// Create sync client
const syncClient = new TLSyncClient({
store,
socket,
presence: atom(null),
onLoad: () => console.log('Connected and loaded'),
onSyncError: (reason) => console.error('Sync failed:', reason),
})
// Changes to store are now automatically synchronized
store.put([{ id: 'shape1', type: 'geo', x: 100, y: 100 }])
// Advanced usage with presence and custom messages
const syncClient = new TLSyncClient({
store,
socket,
presence: atom({ cursor: { x: 0, y: 0 }, userName: 'Alice' }),
presenceMode: atom('full'),
onCustomMessageReceived: (data) => {
if (data.type === 'chat') {
showChatMessage(data.message, data.from)
}
},
onAfterConnect: (client, { isReadonly }) => {
if (isReadonly) {
showNotification('Connected in read-only mode')
}
},
})
Constructor
Creates a new TLSyncClient instance to manage synchronization with a remote server.
Parameters
Name | Description |
---|---|
|
Configuration object for the sync client
|
Methods
close( )
Closes the sync client and cleans up all resources.
Call this method when you no longer need the sync client to prevent memory leaks and close the WebSocket connection. After calling close(), the client cannot be reused.
close(): void
Example
// Clean shutdown
syncClient.close()