Meteoris server to server ddp synchronizer
meteoris:sync is a meteor package to synchronize collections among server using distributed data protocol (DDP). It still under experiment, so becarefull if you planto use it on production,. ^,....,^
Under the hood
Basically, synchronizer synchronize two server collection by communicating via DDP. These concept is similar with DDP connection in general. We have server 'a' as client of server and server 'b' as server of server. When server 'a' has some data insertion, update or deletion, server 'a' will call a method in server 'b' to tell server 'b' that there is data change in server 'a'. So do server 'b' will always publish tracker if anything happen in server 'b' data. This package use background process to run sync task from client to server.
Usage
On the server of server:
1clients = new Mongo.Collection('clients'); 2collectors = new Mongo.Collection('collectors'); 3courses = new Mongo.Collection('courses'); 4 5publisher = Meteoris.Sync.Publisher.instance(collectors, clients); 6method = Meteoris.Sync.Method.instance(collectors); 7 8publisher.register(courses, 'code'); 9method.register(courses, 'code'); 10 11publisher.publish(); 12method.run();
At the code above, we need initialize server publisher.
- collectors is a collection to hold data of server data change.
- clients is a collection to hold data of clients / consumers
- courses is collection that we want to sync. (createdAt and updatedAt fields are mandatory)
Publisher is a class that hold what collections that we want to publish.
Publisher has 3 public method
- before(callback) : Register global hook before collection tell the client something was changed such as authentication or authorization
- register(collection, identifier, before): Register collections so the collections are visible to client. It has 3 parameters, collection is collection to register, identifier is collection identifier except the _id field (it's not support composite key yet) and before is specific collection before hook.
- publish: Publishing publisher after everything are ready.
Method is a class to hold allowed collections to touch by the client.
Method has 4 public method
- before (callback) : Same as publisher
- after (callback) : Same as before but called after
- register(collection, identifier, before, after): same as publisher with addional after callback
- run: same as publish on publisher
On the client of server:
1courses = new Mongo.Collection('courses-client'); 2ddp = DDP.connect('http://localhost:3030'); 3caller = Meteoris.Sync.Caller.instance(ddp); 4subscriber = Meteoris.Sync.Subscriber.instance('DSvjND3QWsbxdhBbC', ddp); 5 6caller.register(courses, 'courses', 'code'); 7caller.call(); 8 9subscriber.register(courses, 'courses', 'code'); 10subscriber.subscribe();
Unlike on the server implementation, we just need to define collections that we want to sync (in this case is courses).
DDP.connect is ddp implementation for remote connection. You can refer to this link to get more detail DDP.connect
Caller is a class to hold collection that we want to push to server.
Caller.instance need 2 parameters. The first one is ddp connection to server and the second one is global additional payload. Additional payload is very usefull to hold additional data such as client's token or client's tenant id.
Caller has 3 public methods
- register(collection, remoteCollection, identifier, payload, after)
- collection is collection instance to register
- remoteCollection is remote collection name in string
- idetifier is unique field
- payload is collection specific additional payload
- after is collection specific after hook
- after(callback)
- callback is global after hook
- call()
- Boot caller
Subscriber is a class to hold collection that we want to keep in touch with server's collection.
Subscriber.instance need 3 parameters. The first one is client id, the second one is ddp connection, and the third one is payload
Subscriber has 3 public method
- after(callback) : same as caller
- register(collection, remoteCollection, identifier, payload, after) : same as caller
- subscribe: same as call on caller
Todo
- Add some tests
- Add composite key support
- Add configurable options
- Add column filtering
Credits
- percolated:synced-cron
- matb33:collection-hooks
- and all above dependency