Sync structured data across clients in real time. Documents within a collection carry monotonically increasing version numbers and are merged using a last-writer-wins (LWW) strategy applied per field, so concurrent edits to different fields of the same document are automatically reconciled. When two clients update the same field simultaneously, a conflict event is emitted so your application can apply custom resolution logic if the default LWW behaviour is not appropriate. Collections group related documents and behave like lightweight database tables.
NoLagSync wraps the @nolag/js-sdk client and manages a lobby that tracks which collaborators are online. Calling joinCollection(name) returns a SyncRoom that subscribes to the changes topic. Every create, update, and delete operation is published to that topic so all subscribers receive it and update their local document cache. The SDK maintains a version counter per document and detects conflicts when an incoming update has a base version lower than the current local version.
Topic
Purpose
Replay
changes
Document create, update, and delete operations with version metadata
import { NoLagSync } from'@nolag/sync'const sync = newNoLagSync('your-access-token')
await sync.connect()
// Join a collection (analogous to a database table or folder)const collection = await sync.joinCollection('tasks')
// Create a document (all collaborators see it instantly)await collection.createDocument('task-001', {
title: 'Design new homepage',
status: 'todo',
assignee: 'alice',
priority: 1,
})
// Update specific fields (version is incremented automatically)await collection.updateDocument('task-001', {
status: 'in-progress',
assignee: 'bob',
})
// Read a document from local cache (no network call)const task = await collection.getDocument('task-001')
console.log(task?.version) // e.g. 2// Read all documents in the collectionconst all = await collection.getAllDocuments()
console.log(`${all.length} documents in collection`)
// Delete a documentawait collection.deleteDocument('task-001')
// React to changes from other collaborators
collection.on('documentCreated', ({ id, data, version }) => {
console.log('New document:', id, data)
})
collection.on('documentUpdated', ({ id, fields, version }) => {
console.log('Updated:', id, fields)
})
collection.on('documentDeleted', ({ id }) => {
console.log('Deleted:', id)
})
// Handle conflicts (last-writer-wins per field, conflict emitted for custom resolution)
collection.on('conflict', ({ id, localVersion, remoteVersion, fields }) => {
console.warn('Conflict on', id, '- remote version wins unless resolved manually')
})
// Know when the local state is fully in sync with the server
collection.on('synced', () => {
console.log('Collection fully synced')
})