-
-
Save vparihar01/11095911 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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