Revisions
-
scttnlsn revised this gist
Jul 31, 2012 . 1 changed file with 11 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,6 +7,15 @@ Setup: > use pubsub > db.createCollection('messages', { capped: true, size: 100000 }) > db.messages.insert({}) $ npm install mongodb Subscribe: $ node subscribe.js Publish: $ mongo > use pubsub > db.messages.insert({ message: 'Hello world', time: Date.now() }) -
scttnlsn revised this gist
Jul 31, 2012 . 1 changed file with 0 additions and 19 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,19 +0,0 @@ -
scttnlsn revised this gist
Jul 30, 2012 . 2 changed files with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -10,9 +10,9 @@ db.open(function(err) { if (err) throw err; setInterval(function() { collection.insert({ foo: 'bar', time: Date.now() }, function(err, message) { if (err) throw err; console.log('published', message._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 charactersOriginal file line number Diff line number Diff line change @@ -20,9 +20,9 @@ db.open(function(err) { var cursor = collection.find(query, options).sort({ $natural: 1 }); (function next() { cursor.nextObject(function(err, message) { if (err) throw err; console.log(message); next(); }); })(); -
scttnlsn revised this gist
Jul 30, 2012 . 1 changed file with 2 additions and 9 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,12 @@ Pub/sub with MongoDB and Node.js === Setup: $ mongo > use pubsub > db.createCollection('messages', { capped: true, size: 100000 }) > db.messages.insert({}) > exit $ npm install mongodb -
scttnlsn revised this gist
Jul 30, 2012 . 1 changed file with 10 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,4 +7,13 @@ Initialize the capped collection: > use pubsub > db.createCollection('messages', { capped: true, size: 100000 }) > db.messages.insert({}) > exit Running: $ npm install mongodb $ node subscriber.js In another terminal: $ node publish.js -
scttnlsn revised this gist
Jul 30, 2012 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ Pub/sub with MongoDB and Node.js === Initialize the capped collection: $ mongo > use pubsub > db.createCollection('messages', { capped: true, size: 100000 }) > db.messages.insert({}) > exit -
scttnlsn created this gist
Jul 30, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ 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(); }); })(); }); }); });