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() })
Updated version -- newer MongoDB versions use Promises instead of callbacks: