Setup:
$ mongo
> 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() })
subscribe.js, line 26: We're trusting that
cursor.nextObject()
will put the callback in the event loop, which is a reasonable assumption for an asynchronous call, but it still relies on the implementation. Ifcursor.nextObject()
calls the callback directly, we will eventually have a "Maximum call stack exceeded" error. Just to be on the safe side, we could change this line toprocess.nextTick(() => next());
.