It's a Meteor compatible DDP client, based on MarsDB. It supports methods, pub/sub and collection operations. It is very similar to Meteor, but it also have some killer features...
Features
- Cache collections – with LocalForage, for example
- Auto subcribe/unsubscribe - see examples
- Smart subscriptions – any subscription will be stopped only after 15 sec delay.
- Framework agnostic
- Works in any JS environment – browser, Node.JS, Electron, NW.js, Cordova
WARNING
It's only a concept until 1.0. Use it for your own risk.
Examples
Basic example
The repository comes with a simple example. To try it out:
git clone https://github.com/c58/marsdb-sync-client.git
cd marsdb-sync-client/example && npm install
npm start
Then, just point your browser at http://localhost:3000
.
Usage with Meteor server and LocalForage
marsdb-sync-client
is a DDP client, so it should work well with Meteor server.
But it have an extension for syncing local cache with a server side. The extension is
implemented by marsdb-sync-server
and is follow:
- Each collection have additional server method called
/${myCollection}/sync
- This method invoked by MarsSync client on init stage for each collection
- Method invoked with one argument: list of all available document ids in a local cache
- Method must return a sublist of given ids that is NOT presented in a server anymore (deleted ids).
You should implement it by yourself for each collection in Meteor's server-side code.
In the future it might be a package for Meteor (it would be great if you implement it).
Example of a sync method for some collection:
Posts = new Meteor.Collection('posts');
Meteor.methods({
'/posts/sync': function(remoteIds) {
const existingDocs = Posts.find({_id: {$in: remoteIds}}, {fields: {_id: 1}}).fetch();
const existingIdsSet = new Set(existingDocs.map(doc => doc._id));
return remoteIds.filter(id => !existingIdsSet.has(id));
}
});
Configure a client
import Collection from 'marsdb';
import * as MarsSync from 'marsdb-sync-client';
MarsSync.configure({ url: 'ws://localhost:3000' });
const posts = new Collection('posts');
const observer = posts.find(
{author: 'me'},
{sub: ['postsByAuthor', 'me']}
).observe((posts) => {
});
observer.stop();
Wait for subscription ready
const posts = new Collection('posts');
posts.find(
{author: 'me'},
{sub: ['postsByAuthor', 'me'], waitReady: true}
).observe((posts) => {
});
Decide when to use cache
const posts = new Collection('posts');
posts.find(
{author: 'me'},
{sub: ['postsByAuthor', 'me'], tryCache: true}
).observe((posts) => {
});
posts.find(
{author: 'not_me'},
{sub: ['postsByAuthor', 'not_me'], tryCache: (posts) => true}
).observe((posts) => {
});
Methods and subscriptions
import * as MarsSync from 'marsdb-sync-client';
MarsSync.call('myMethod', 1, 2, 3).result().then((res) => {
}).updated().then(() => {
});
MarsSync.apply('myMethod', [1, 2, 3])
const sub = MarsSync.subscribe('myPublisher', 1, 2, '3th arg');
sub.ready().then(() => {
}).stopped().then(() => {
});
sub.stop();
Roadmap
- More examples of usage and tests
- Documentation
Contributing
I'm waiting for your pull requests and issues.
Don't forget to execute gulp lint
before requesting. Accepted only requests without errors.
License
See License