Code Monkey home page Code Monkey logo

marsdb-sync-client's Introduction

Build Status npm version Coverage Status Dependency Status

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:

// /server/collections/Posts.js
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';

// Setup marsdb-sync-client
MarsSync.configure({ url: 'ws://localhost:3000' });

// User your collections
const posts = new Collection('posts');
const observer = posts.find(
  {author: 'me'},
  {sub: ['postsByAuthor', 'me']}
).observe((posts) => {
  // Subscribe to "postsByAuthor" publisher and update any time when
  // some documents added (but with debounce)
});

// When you stop all observers of a cursor
// subscription will be automatically stopped
// (after 15 sec for optimal client/server communication)
observer.stop();

Wait for subscription ready

// Sometimes you need to show a result only when
// all documents of a subscription received.
// There is special options "waitReady" for this.
const posts = new Collection('posts');
posts.find(
  {author: 'me'},
  {sub: ['postsByAuthor', 'me'], waitReady: true}
).observe((posts) => {
  // Subscribe to "postsByAuthor" publisher and wait until
  // subscription ready (observer called only when sub ready)
});

Decide when to use cache

// Cache is good, but sometimes you need to decide when to use
// cache, and when to wait new data from the server...
const posts = new Collection('posts');
posts.find(
  {author: 'me'},
  {sub: ['postsByAuthor', 'me'], tryCache: true}
).observe((posts) => {
  // Subscribe to "postsByAuthor" publisher and try
  // to get posts from cache. If "tryCache" is true, then
  // cache is used when it's not empty array or not empty object.
  // In other cases it will wait subscription ready.
});

posts.find(
  {author: 'not_me'},
  {sub: ['postsByAuthor', 'not_me'], tryCache: (posts) => true}
).observe((posts) => {
  // Subscribe to "postsByAuthor" publisher and try
  // to get posts from cache. If "tryCache" is a function, then
  // the function will be called with cache result. If function returns
  // true, then cache will be used.
});

Methods and subscriptions

import * as MarsSync from 'marsdb-sync-client';

MarsSync.call('myMethod', 1, 2, 3).result().then((res) => {
  // Result of the method in "res"
}).updated().then(() => {
  // Invoked when "updated" message received.
});

// Similar for "apply"
MarsSync.apply('myMethod', [1, 2, 3])

// You can also subscribe just like in Meteor
const sub = MarsSync.subscribe('myPublisher', 1, 2, '3th arg');
sub.ready().then(() => {
  // When ready
}).stopped().then(() => {
  // When stopped
});
// Stop the subscription
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

marsdb-sync-client's People

Contributors

c58 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

rongfengliang

marsdb-sync-client's Issues

"watch" does not exist as task

Watch does not exist as task:

Task watch is not configured as a task on gulp.  If this is a submodule, you may need to use require('run-sequence').use(gulp).

either remove 'watch' from the list of tasks in runSequence or create a watch-function/file.

Error: Couldn't find preset "es2015" relative to directory

Hi,

While running the example i get the error

Error: Couldn't find preset "es2015" relative to directory "E:\\Research\\1.Workspace\\marsdb-sync-client"
    at E:\Research\1.Workspace\marsdb-sync-client\example\node_modules\babel-core\lib\transformation\file\options\option-manager.js:299:19
    at Array.map (native)
    at OptionManager.resolvePresets (E:\Research\1.Workspace\marsdb-sync-client\example\node_modules\babel-core\lib\transformation\file\options\option-manager.js:270:20)
    at OptionManager.mergePresets (E:\Research\1.Workspace\marsdb-sync-client\example\node_modules\babel-core\lib\transformation\file\options\option-manager.js:259:10)
    at OptionManager.mergeOptions (E:\Research\1.Workspace\marsdb-sync-client\example\node_modules\babel-core\lib\transformation\file\options\option-manager.js:244:14)
    at OptionManager.init (E:\Research\1.Workspace\marsdb-sync-client\example\node_modules\babel-core\lib\transformation\file\options\option-manager.js:374:12)
    at compile (E:\Research\1.Workspace\marsdb-sync-client\example\node_modules\babel-register\lib\node.js:103:45)
    at loader (E:\Research\1.Workspace\marsdb-sync-client\example\node_modules\babel-register\lib\node.js:144:14)
    at Object.require.extensions.(anonymous function) [as .js] (E:\Research\1.Workspace\marsdb-sync-client\example\node_modules\babel-register\lib\node.js:154:7)
    at Module.load (module.js:487:32)

npm debug log#

0 info it worked if it ends with ok
1 verbose cli [ 'D:\Softwares\nodejs\node.exe',
1 verbose cli 'D:\Softwares\nodejs\node_modules\npm\bin\npm-cli.js',
1 verbose cli 'start' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle @~prestart: @
6 silly lifecycle @~prestart: no script for prestart, continuing
7 info lifecycle @~start: @
8 verbose lifecycle @~start: unsafe-perm in lifecycle true
9 verbose lifecycle @~start: PATH: D:\Softwares\nodejs\node_modules\npm\bin\node-gyp-bin;E:\Research\1.Workspace\marsdb-sync-client\example\node_modules.bin;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;D:\Softwares\nodejs;D:\Softwares\Git\cmd;D:\Softwares\Tortoise\bin;C:\Program Files\Intel\WiFi\bin;C:\Program Files\Common Files\Intel\WirelessCommon;C:\Users\p10330680\AppData\Local\Microsoft\WindowsApps;C:\Users\p10330680\AppData\Local.meteor;C:\Users\p10330680\AppData\Roaming\npm
10 verbose lifecycle @~start: CWD: E:\Research\1.Workspace\marsdb-sync-client\example
11 silly lifecycle @~start: Args: [ '/d /s /c', 'babel-node ./server.js' ]
12 silly lifecycle @~start: Returned: code: 1 signal: null
13 info lifecycle @~start: Failed to exec start script
14 verbose stack Error: @ start: babel-node ./server.js
14 verbose stack Exit status 1
14 verbose stack at EventEmitter. (D:\Softwares\nodejs\node_modules\npm\lib\utils\lifecycle.js:255:16)
14 verbose stack at emitTwo (events.js:106:13)
14 verbose stack at EventEmitter.emit (events.js:191:7)
14 verbose stack at ChildProcess. (D:\Softwares\nodejs\node_modules\npm\lib\utils\spawn.js:40:14)
14 verbose stack at emitTwo (events.js:106:13)
14 verbose stack at ChildProcess.emit (events.js:191:7)
14 verbose stack at maybeClose (internal/child_process.js:877:16)
14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
15 verbose pkgid @
16 verbose cwd E:\Research\1.Workspace\marsdb-sync-client\example
17 error Windows_NT 10.0.14393
18 error argv "D:\Softwares\nodejs\node.exe" "D:\Softwares\nodejs\node_modules\npm\bin\npm-cli.js" "start"
19 error node v6.9.1
20 error npm v3.10.8
21 error code ELIFECYCLE
22 error @ start: babel-node ./server.js
22 error Exit status 1
23 error Failed at the @ start script 'babel-node ./server.js'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error babel-node ./server.js
23 error You can get information on how to open an issue for this project with:
23 error npm bugs
23 error Or if that isn't available, you can get their info via:
23 error npm owner ls
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]

Let me know if you need more information.

Tag 0.2.11

Please, create the 0.2.11 tag. Because I'm using jspm, and the dependencies only installed from a tagged version.

$ jspm install github:c58/marsdb-sync-client
     Looking up github:c58/marsdb-sync-client
     Updating registry cache...
     Downloading github:c58/[email protected]

warn github:c58/[email protected] dependency installs skipped as it's a GitHub package with no registry property set.
     If the dependencies aren't needed ignore this message. Alternatively set a registry or dependencies override or use the npm registry version at jspm install npm:marsdb-sync-client@^0.1.0 instead.

ok   Installed c58/marsdb-sync-client as github:c58/marsdb-sync-client@^0.1.0 (0.1.0)
ok   Install tree has no forks.

ok   Install complete.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.