Skip to main content

Sync Clients

kSync provides multiple synchronization clients to sync data between instances. Choose between real-time WebSocket synchronization or Git-like pull/push synchronization.

Sync Client Interface

WebSocketSyncClient

Real-time bidirectional synchronization using WebSockets.

Constructor

Parameters:
  • url (string): WebSocket server URL
  • options (optional):
    • auth?: Authentication configuration
    • reconnect? (boolean): Auto-reconnect on disconnect (default: true)
    • heartbeat? (number): Heartbeat interval in ms (default: 30000)
    • compression? (boolean): Enable compression (default: false)

Authentication

Methods

connect()

Connects to the WebSocket server.

disconnect()

Disconnects from the WebSocket server.

send(event)

Sends an event to connected peers.

Event Listeners

Complete Example

GitSyncClient

Git-like pull/push synchronization for offline-first applications.

Constructor

Options:
  • url (string): Server URL for sync operations
  • auth?: Authentication configuration
  • branch? (string): Branch name (default: ‘main’)
  • pullInterval? (number): Auto-pull interval in ms (default: 0 = disabled)
  • compression? (boolean): Enable compression (default: true)

Methods

pull()

Pulls latest events from the server.
Returns:

push()

Pushes local events to the server.
Returns:

sync()

Performs a full sync (pull + push).

clone(url, branch?)

Clones a repository from a remote server.

Conflict Resolution

Handle merge conflicts when events conflict:

Branching

Work with multiple branches:

Complete Example

P2PSyncClient

Peer-to-peer synchronization without a central server.

Constructor

Options:
  • peerId (string): Unique identifier for this peer
  • discoveryMethod (‘mdns’ | ‘dht’ | ‘manual’): How to discover peers
  • port? (number): Port for P2P communication
  • encryption? (boolean): Enable end-to-end encryption

Methods

start()

Starts the P2P node and begins peer discovery.

connectToPeer(peerId)

Manually connect to a specific peer.

broadcast(event)

Broadcasts an event to all connected peers.

Example

Sync Strategies

Configure different synchronization strategies:

Event-Level Sync

Sync individual events as they occur:

Batch Sync

Sync events in batches for better performance:

State-Based Sync

Sync entire state snapshots:

Sync Filters

Filter which events to sync:

Sync Middleware

Add middleware to transform events during sync:

Monitoring and Debugging

Monitor sync performance and debug issues:

Custom Sync Client

Create a custom sync client by implementing the interface: