2.0.1 • Published 9 years ago

mesh-pubnub v2.0.1

Weekly downloads
5
License
ISC
Repository
github
Last release
9 years ago

Build Status Coverage Status Dependency Status

A streamable interface for Pubnub. This library also works nicely with mesh, and other mesh adapters.

Example

var pubnub      = require("mesh-pubnub");
var memory      = require("mesh-memory");
var mesh        = require("mesh");

var mem = mesh.tailable(memory());

var pubnub = mesh.reject("load", pubnub({
  subscribeKey: "sub key"
  publishKey: "pub key",
  channel: "streamChannel"
}, mem);


mem(mesh.op("tail")).pipe(mesh.open(pubnub));

db pubnub(options, responseBus)

Creates a new pubnub streamer.

  • options
    • subscribeKey - your pubnub subscription key
    • publishKey - your pubnub publish key
    • channel - (optional) the channel to subscribe to
  • responseBus - set of commands to reject - default is [load]
var pubStream = pubnub({
  subscribeKey: "sub key"
  publishKey: "pub key",
  channel: "streamChannel"
}, memoryBus);

// does not get broadcasted
pubStream(mesh.operation("anotherCommandToIgnore"));

db.addChannel(channel)

adds a new channel to subscribe to.

pubStream.addChannel(mesh.operation("someChannel"));
pubStream.addChannel(mesh.operation("anotherChannel")_;

stream.Readable db(operationName, options)

Publishes a new operation to pubnub.

pubStream({ name: "hello", data: { name: "world" }});
pubStream({ name "doSomething", data: { name: "world" }});

stream.Readable db(tail, filter)

Tails a remote operation. This is your subscription function.

db({ name: "tail" }).on("data", function(operation) {

});

Or you can do something like synchronizing databases between clients:

var mesh   = require("mesh");
var loki   = require("mesh-loki");
var pubnub = require("mesh-pubnub");

var pubdb = pubnub({
  subscribeKey: "sub key"
  publishKey: "pub key",
  channel: "streamChannel"
});

var db = mesh.tailable(loki());

// listen for local operations on lokidb - pass to pubnub
db(mesh.operation("tail")).pipe(mesh.open(pubdb));

// listen for remote operations on pubnub - pass to lokidb
pubdb(mesh.operation("tail")).pipe(mesh.open(db));

// stored in loki & synchronized across clients
db(mesh.operation("insert", { data: { name: "Juice" }}));