Name: vubsub
Owner: Vivocha
Description: Pub/Sub for Node.js and MongoDB
Created: 2013-06-07 06:22:51.0
Updated: 2017-05-08 20:47:15.0
Pushed: 2017-05-15 16:26:05.0
Homepage: null
Size: 24
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
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:
vubsub uses Q, Kris Kowal's implementation of Promises.
install vubsub
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).
vubsub = require('vubsub')
MongoClient = require('mongodb').MongoClient
oClient.connect('mongodb://localhost/test', { auto_reconnect: true }, function(err, db) {
(err || !db) throw new Error('failed to connect to the db');
bsub.create(db, { ns: 'myNamespace' }).then(function(client) {
console.log('Client connected: ' + client.id);
;
To create a channel and listen to events on the channel:
ub.create(db, { ns: 'myNamespace' }).then(function(client) {
turn client.channel('myChannel' /*, you can pass here the id of the last received message */);
hen(function(channel) {
nsole.log('Created channel ' + channel.name);
annel.on('myEvent', function(event) {
console.log('Received event', event);
;
To send a message to a channel:
nel.send('myEvent', { a: 1, b: 2}).then(function() {
nsole.log('Message sent');