Skip to content

v5.1.0

Choose a tag to compare

@jamesgpearce jamesgpearce released this 19 Jul 22:31
· 890 commits to main since this release

This release lets you persist data on a server using the createWsServer function. This makes it possible for all clients to disconnect from a path, but, when they reconnect, for the data to still be present for them to sync with.

This is done by passing in a second argument to the createWsServer function that creates a Persister instance (for which also need to create or provide a MergeableStore) for a given path:

import {WebSocketServer} from 'ws';
import {createFilePersister} from 'tinybase/persisters/persister-file';
import {createMergeableStore} from 'tinybase';
import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';

const persistingServer = createWsServer(
  new WebSocketServer({port: 8051}),
  (pathId) => createFilePersister(createMergeableStore(), pathId + '.json'),
);

This is a very crude (and not production-safe!) example, but demonstrates a server that will create a file, based on any path that clients connect to, and persist data to it. See the createWsServer function documentation for more details.

This implementation is still experimental so please kick the tires!

There is one small breaking change in this release: the functions for creating Synchronizer objects can now take optional onSend and onReceive callbacks that will fire whenever messages pass through the Synchronizer. See, for example, the createWsSynchronizer function. These are suitable for debugging synchronization issues in a development environment.