offline-sync
Differential Synchronization based method with offline mode implemented, using diffsyncpatch to merge json objects.
Prerequisites
Typescript project or a support of es2017 is required.
Installation
$ npm install --save offline-sync
const offlineSync = ;
Client usage
Options
// Id among which state is synchronized between clientslet roomId = 'shared_document'; // Store for saving data in offline modelet store = 'local_storage_key'; // On significant state conflicts this function is called to let user manually perform mergelet { // show screen similiar to git's diffviewers // This can consist of 3 richText editors, like Quill let leftEditor middleEditor rightEditor submitButton = ; leftEditor; leftEditoreditor; // make read-only // This is where the merging happens middleEditor; rightEditor; rightEditoreditor; // make read-only // Wait for submit click let mergeSubmit = { submitButton; }; return mergeSubmit;}; // https://github.com/benjamine/jsondiffpatch#options// Optional: defaults to empty objectlet jsondiffpatchOptions = {}; // Url on which commands will be syncrhonized (eg. http://server.com/state/synchronization/join)// Optional: defaults to empty stringlet endpointUrl = 'state/synchronization';
Example
let client = roomId store onUserMerge jsondiffpatchOptions endpointUrl; try let document = await client; // change the document JSON object any way you want ... // After some document changes: await client; ; ... // document will now be synchronized between client and server // this synchronization is guaranteed, even after packets get dropped or connection lost for some time, // the sync will recover await client; ; ... // To fetch the server changes also use sync await client; ; ... // Await is needed to wait for syncing to finish, multiple syncs cannot run in parallel client; // runs client; // prints to console that syncing is already in progress catcherror console;
Store interface
To implement your own client side store interface has to be met:
/** * Interface for client side data persistence. Can use localstorage, sessionstorage, indexDb, cookies * or just in memory, if offline mode is not needed. */
The structure of Document
can change in time, however it is guaranteed to be of object
type.
Server Usage
Options
// Data adapter for storing data on the server, some database adapter is expected herelet adapter = ; // https://github.com/benjamine/jsondiffpatch#options// Optional: defaults to empty objectlet jsondiffpatchOptions = {}; // Url on which commands will be syncrhonized (eg. http://server.com/state/synchronization/join)// Optional: defaults to empty stringlet endpointUrl = '/state/synchronization';
Example
let server = adapter jsondiffpatchOptions endpointUrl;// List of endpoints and their handlers that you can use in any http server implementationlet endpoints = server; endpoints;
Data Adapter Interface
To implement a way server side data is stored, create a class using this interface:
/** * Interface for communicating with persistence layer, saving entities of type Document and State * Server side provides no caching, so if you require it, please implement it here. */