Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Pub/sub for Node.js and MongoDB

tag: v0.1.2

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 example
Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore Initial commit April 04, 2012
Octocat-spinner-32 LICENSE Added LICENSE June 29, 2012
Octocat-spinner-32 Makefile Initial commit April 04, 2012
Octocat-spinner-32 README.md
Octocat-spinner-32 package.json
README.md

mubsub

Mubsub is a pub/sub implementation for Node.js and MongoDB. It utilizes Mongo's capped collections and tailable cursors to notify subscribers of inserted documents that match a given query.

Example

var mubsub = require('mubsub');

mubsub.connect('mongodb://localhost:27017/mubsub_example');

var channel = mubsub.channel('test');

channel.subscribe({ foo: 'bar' }, function(doc) {
    console.log(doc.foo); // => 'bar'
});

channel.subscribe({ foo: 'baz' }, function(doc) {
    console.log(doc.foo); // => 'baz'
});

channel.publish({ foo: 'bar' });
channel.publish({ foo: 'baz' });

Usage

Channels

A channel maps one-to-one with a capped collection (Mubsub will create these if they do not already exist in the database). Optionally specify the byte size of the collection or/and max number of documents in the collection when creating a channel:

var channel = mubsub.channel('foo', { size: 100000, max: 500 });

Subscribe

var subscription = channel.subscribe(query, callback);

Subscriptions register a callback to be called whenever a document matching the specified query is inserted (published) into the collection (channel). You can omit the query to match all inserted documents. To later unsubscribe a particular callback, call unsubscribe on the returned subscription object:

subscription.unsubscribe();

Publish

channel.publish(doc, callback);

Publishing a document simply inserts the document into the channel's capped collection. Note that Mubsub will remove any specified document _id as the natural ordering of ObjectIds is used to ensure subscribers do not receive notifications of documents inserted in the past. Callback is optional.

Disconnect

mubsub.disconnect();

Closes the MongoDB connection.

Events

channel.on('error', function(err) {
    console.log(err);
});

Install

npm install mubsub

Tests

make test

You can optionally specify the MongoDB URI to be used for tests:

MONGODB_URI=mongodb://localhost:27017/mubsub_tests make test
Something went wrong with that request. Please try again.