Skip to content

Instantly share code, notes, and snippets.

@vparihar01
Forked from scttnlsn/README.md
Created April 19, 2014 20:07

Revisions

  1. @scttnlsn scttnlsn revised this gist Jul 31, 2012. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions README.md
    Original 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({})
    > exit

    $ npm install mongodb
    $ npm install mongodb

    Subscribe:

    $ node subscribe.js

    Publish:

    $ mongo
    > use pubsub
    > db.messages.insert({ message: 'Hello world', time: Date.now() })
  2. @scttnlsn scttnlsn revised this gist Jul 31, 2012. 1 changed file with 0 additions and 19 deletions.
    19 changes: 0 additions & 19 deletions publish.js
    Original file line number Diff line number Diff line change
    @@ -1,19 +0,0 @@
    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, message) {
    if (err) throw err;
    console.log('published', message._id);
    });
    }, 2000);
    });
    });
  3. @scttnlsn scttnlsn revised this gist Jul 30, 2012. 2 changed files with 4 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions publish.js
    Original 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) {
    collection.insert({ foo: 'bar', time: Date.now() }, function(err, message) {
    if (err) throw err;
    console.log('published', doc._id);
    console.log('published', message._id);
    });
    }, 2000);
    });
    4 changes: 2 additions & 2 deletions subscribe.js
    Original 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, doc) {
    cursor.nextObject(function(err, message) {
    if (err) throw err;
    console.log(doc);
    console.log(message);
    next();
    });
    })();
  4. @scttnlsn scttnlsn revised this gist Jul 30, 2012. 1 changed file with 2 additions and 9 deletions.
    11 changes: 2 additions & 9 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,12 @@
    Pub/sub with MongoDB and Node.js
    ===

    Initialize the capped collection:
    Setup:

    $ mongo
    > 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
    $ npm install mongodb
  5. @scttnlsn scttnlsn revised this gist Jul 30, 2012. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion README.md
    Original 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
    > exit

    Running:

    $ npm install mongodb
    $ node subscriber.js

    In another terminal:

    $ node publish.js
  6. @scttnlsn scttnlsn revised this gist Jul 30, 2012. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions README.md
    Original 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
  7. @scttnlsn scttnlsn created this gist Jul 30, 2012.
    19 changes: 19 additions & 0 deletions publish.js
    Original 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);
    });
    });
    31 changes: 31 additions & 0 deletions subscribe.js
    Original 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();
    });
    })();
    });
    });
    });