GithubHelp home page GithubHelp logo

vubsub's Introduction

vubsub

Pub/Sub for Node.js and MongoDB

vubsub is a Pub/Sub implementation for Node.js on top of MongoDB tailable cursors. vubsub was inspired by mubsub.

The main differences between vubsub and mubsub are:

  • Active clients are tracked via a dedicated clients collection
  • You can specify a namespace for each client (or group of clients): a namespace is a mapped to a collection (ns_namespace) where all the messages for that namespace are stored
  • When connecting a client to a channel, you can specify the _id of the last received message and restart receiving message from that point.

vubsub uses Q, Kris Kowal's implementation of Promises.

How to Install

npm install vubsub

How to use

To create a new client, simply call the create function passing a db instance and, optionally, a metadata object. If the metadata contains the ns key, it'll be used as the namespace for the client (i.e. the client will be linked to a collection named ns_namespace). The metadata is stored, together with the client id in the clients collection (you can you this to keep track of your clients).

var vubsub = require('vubsub')
  , MongoClient = require('mongodb').MongoClient

MongoClient.connect('mongodb://localhost/test', { auto_reconnect: true }, function(err, db) {
  if (err || !db) throw new Error('failed to connect to the db');
  
  vubsub.create(db, { ns: 'myNamespace' }).then(function(client) {
    console.log('Client connected: ' + client.id);
  });
});

To create a channel and listen to events on the channel:

vubsub.create(db, { ns: 'myNamespace' }).then(function(client) {
  return client.channel('myChannel' /*, you can pass here the id of the last received message */);
}).then(function(channel) {
  console.log('Created channel ' + channel.name);
  channel.on('myEvent', function(event) {
    console.log('Received event', event);
  });
});

To send a message to a channel:

channel.send('myEvent', { a: 1, b: 2}).then(function() {
  console.log('Message sent');
});

vubsub's People

Contributors

0xfede avatar

Watchers

 avatar

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.