Home

Awesome

cycle-deepstream

Build Status npm version Coverage Status Join the chat at https://gitter.im/EnigmaCurry/cycle-deepstream

A Cycle.js driver for the deepstream.io javascript client.

Allows realtime communication between multiple clients and your cluster of deepstream servers. A self-hosted firebase replacement.

simple example advanced example

(above: the simple example running in three browser windows, and the advanced example simulating two mobile devices.)

Run Demo

Get the code:

git clone https://github.com/EnigmaCurry/cycle-deepstream.git
cd cycle-deepstream
npm install

Run the simple example:

npm run simple

Or run the advanced demo:

npm run advanced

Usage Notes

No formal docs exist for this yet. This driver implements most of the deepstream API methods. In Cycle.js you never use imperative calls directly (code with side-effects), so cycle-deepstream wraps these calls into a driver with an API that uses plain-javascript objects to define actions (actions.ts).

For instance, calling record.subscribe returns an object that just describes what action to perform:

> const {actions} = require('cycle-deepstream')
> actions.record.subscribe('some-record')
{ action: 'record.subscribe',
  name: 'some-record',
  events: {},
  scope: undefined }

This action describes the appropriate deepstream API method to call and some additional arguments. In this case they are:

This plain object is sent to the driver in it's input stream and processed by the driver asynchronously. When a deepstream event comes from the server, the driver passes this information to the output stream and back into your cycle application. Events of this sort look like this:

{ event: 'record.change',
  name: 'some-record',
  data: {
    foo: 'bar'
  },
  scope: undefined }

This event is the first response to our subscribe action above, consisting of the data of the existing content of the 'some-record' object in deepstream's database. Subseqent events would be more 'record.change' events or possibly 'record.delete' etc.

There's a helper function to apply scopes to actions:

> const {actions} = require('cycle-deepstream')
> const scope = actions.scope()    /* This returns a function, a 'scope applier' */
> scope(actions.record.get('some-record'))
{ action: 'record.get',
  name: 'some-record',
  scope: 'j1jlj39z5ue7chjjdi4g' }    /* A randomly generated scope ID */
> scope.scope   /* The scope ID is available on the object, even though it's a function */
'j1jlj39z5ue7chjjdi4g'

The events you receive for the record will now have the scope j1jlj39z5ue7chjjdi4g applied to them so that you can filter on it in your own code.

There are two examples that show general usage. The end-to-end test show comprehensive usage.

Features

Deepstream API Implementation: