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.
var mubsub = ;var client = ;var channel = client;client;channel;channel;channel;channel;channel;
You can pass a Db instance or a URI string. For more information about the URI format visit http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html
var mubsub = ;// Using a URIvar client = ;// Passing a MongoDB driver `Db` instance directly.var client = ;
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 and/or the max number of documents in the collection when creating a channel.
WARNING: You should not create lots of channels because Mubsub will poll from the cursor position.
var channel = client;
sizemax size of the collection in bytes, default is 5mb
maxmax amount of documents in the collection
retryIntervaltime in ms to wait if no docs are found, default is 200ms
recreaterecreate the tailable cursor when an error occurs, default is true
WARNING: Don't remove collections with running publishers. It's possible for
mongod to recreate the collection on the next insert (before Mubsub has the chance to do so). If this happens the collection will be recreated as a normal, uncapped collection.
var subscription = channel;
Subscriptions register a callback to be called whenever a document matching the specified event is inserted (published) into the collection (channel). You can omit the event to match all inserted documents. To later unsubscribe a particular callback, call
unsubscribe on the returned subscription object:
Publishing a document simply inserts the document into the channel's capped collection. A callback is optional.
The following events will be emitted:
// The given event was publishedchannel;// Any event was publishedchannel;// Document was insertedchannel;// Mubsub is ready to receive new documentschannel;// Connection errorclient;// Channel errorchannel;
Closes the MongoDB connection.
npm install mubsub
You can optionally specify the MongoDB URI to be used for tests:
MONGODB_URI=mongodb://localhost:27017/mubsub_tests make test