0.1.1 • Published 11 years ago

level-pubsub v0.1.1

Weekly downloads
3
License
-
Repository
github
Last release
11 years ago

USAGE

level-pubsub accepts any stream. Here is a contrived example using engine.io. See the readme for an example that uses tcp.

Server

Serve this code from a node program...

var pubsub = require('level-pubsub');
var Engine = require('engine.io-stream');
var db = require('level')(__dirname + '/subscriptions', { valueEncoding: 'json' });
var httpserver = require('http').createServer().listen(8000);
var opts = {};

opts.access = function() {};
opts.auth = function(args) {

  // lets only allow clients who pass the argument `foo` 
  // with the value of `bar` to subscribe to stuff.

  if (args.foo == 'bar') { 
   return true; 
  } 
};

pubsub.createServer(db, opts, function(err, stream) {
  var engine = Engine(function (con) {
    con.pipe(stream).pipe(con);
  });
  engine.attach(server, '/pubsub');
});

Client

Client 1 (Part of a browserify bundle)

var engine = require('engine.io-stream');
var pubsub = require('level-pubsub');
var args = { foo: 'bar' };

pubsub.createClient(engine('/pubsub'), args, function(err, client) {
  if (err) {
    return console.log(err);
  }
  client.subscribe('some-channel').on('data', function(data) {
    // data = { value: 'hello, world' }
  });
});

Client 2 (Part of a browserify bundle)

var engine = require('engine.io-stream');
var pubsub = require('level-pubsub');
var args = { foo: 'bar' };

pubsub.createClient(engine('/pubsub'), args, function(err, client) {
  if (err) {
    return console.log(err);
  }
  client.publish('some-channel', { value: 'hello, world' });
});