TinyBase logoTinyBase

Synchronizer

A Synchronizer object lets you synchronize MergeableStore data with another TinyBase client or system.

This is useful for sharing data between users, or between devices of a single user. This is especially valuable when there is the possibility that there has been a lack of immediate connectivity between clients and the synchronization requires some negotiation to orchestrate merging the MergeableStore objects together.

Creating a Synchronizer depends on the choice of underlying medium over which the synchronization will take place. Options include the createWsSynchronizer function (for a Synchronizer that will sync over web-sockets), and the createLocalSynchronizer function (for a Synchronizer that will sync two MergeableStore objects in memory on one system). The createCustomSynchronizer function can also be used to easily create a fully customized way to send and receive the messages of the synchronization protocol.

Note that, as an interface, it is an extension to the Persister interface, since they share underlying implementations. Think of a Synchronizer as 'persisting' your MergeableStore to another client (and vice-versa).

Example

This example creates two empty MergeableStore objects, creates a LocalSynchronizer for each, and starts synchronizing them. A change in one becomes evident in the other.

import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
import {createMergeableStore} from 'tinybase';

const store1 = createMergeableStore();
const store2 = createMergeableStore();

const synchronizer1 = createLocalSynchronizer(store1);
const synchronizer2 = createLocalSynchronizer(store2);

await synchronizer1.startSync();
await synchronizer2.startSync();

store1.setTables({pets: {fido: {species: 'dog'}}});
// ...
console.log(store2.getTables());
// -> {pets: {fido: {species: 'dog'}}}

store2.setRow('pets', 'felix', {species: 'cat'});
// ...
console.log(store1.getTables());
// -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}

synchronizer1.destroy();
synchronizer2.destroy();

Since

v5.0.0

Getter methods

Listener methods

Lifecycle methods

Load methods

Save methods

Synchronization methods

Development methods