pouch-websocket-sync
Sync several PouchDBs through websockets.
Supports reconnection, negotiation and authentication.
Demo
See here an example todo application using React and Redux.
Install
$ npm install pouch-websocket-sync --save
Server
var PouchSync = ;var http = ;var httpServer = http;var server = PouchSync;httpServer; { if credentialstoken == 'some token' ; else ; };
Client
var websocket = ;var PouchSync = ; var db = 'todos';var client = PouchSync;var sync = client; client;
API
PouchWebsocketSync.createServer(httpServer, onRequest)
Creates and returns a websocket server.
Arguments:
httpServer
: an HTTP server to bind toonRequest
: a function to be called when a client requests access to a database. This function must have the following signature:
The arguments to expect on this function are:
credentials
: arbitrary, whatever the client sends as credentials. Defaults toundefined
.dbName
: the name of the database to sync into as being requested by the client.callback
: the callback to call once the request is to be granted or denied. If there is a problem with the requests (invalid credentials or other error), you should pass an error as first arguments. If, otherwise, the request for a database is to proceed, you should passnull
orundefined
as the first argument and a PouchDB database as the second.
Example of an onRequest
function:
{ if credentialstoken == 'some token' ; else ; };
PouchWebsocketSync.createClient()
Creates and returns a webocket sync client.
client.connect(address)
Connect to a given websocket address.
address
a websocket address, likewss://somehost:3000
client.sync(database, options)
Start syncing the given database. Arguments:
database
: an instance of a PouchDB databaseoptions
: an object containing the follow keys and values:- remoteName: remote database name. Defaults to the
database
name. - PouchDB: PouchDB constructor. Defaults to
database.constructor
.
- remoteName: remote database name. Defaults to the
Returns a sync object.
client events
client // when connectsclient // when gets disconnectedclient // when starts attempting to reconnect
Sync
sync.cancel()
Cancel this sync.
client.end() or client.destroy()
sync events
syncsyncsyncsyncsyncsync
License
ISC