Skip to content

Instantly share code, notes, and snippets.

@vparihar01
Forked from scttnlsn/README.md
Created April 19, 2014 20:07
Show Gist options
  • Save vparihar01/11095911 to your computer and use it in GitHub Desktop.
Save vparihar01/11095911 to your computer and use it in GitHub Desktop.
var mongo = require('mongodb');
var server = new mongo.Server('localhost', 27017);
var db = new mongo.Db('pubsub', server);
db.open(function(err) {
if (err) throw err;
db.collection('messages', function(err, collection) {
if (err) throw err;
setInterval(function() {
collection.insert({ foo: 'bar', time: Date.now() }, function(err) {
if (err) throw err;
console.log('published', doc._id);
});
}, 2000);
});
});
var mongo = require('mongodb');
var server = new mongo.Server('localhost', 27017);
var db = new mongo.Db('pubsub', server);
db.open(function(err) {
if (err) throw err;
db.collection('messages', function(err, collection) {
if (err) throw err;
var latest = collection.find({}).sort({ $natural: -1 }).limit(1);
latest.nextObject(function(err, doc) {
if (err) throw err;
var query = { _id: { $gt: doc._id }};
var options = { tailable: true, awaitdata: true, numberOfRetries: -1 };
var cursor = collection.find(query, options).sort({ $natural: 1 });
(function next() {
cursor.nextObject(function(err, doc) {
if (err) throw err;
console.log(doc);
next();
});
})();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment