One of the best features in CouchDB is the change notifications.
Basically, it’s a Comet style push service that informs you of updates to a database. This is great when you’re building Browser apps because you get Comet for free!
However, one thing that’s missing is being able to write some code in your design document that consumes these changes on the backend and doesn’t depend on another active client. To remedy this situation I decided to write a generic change consumer that you could point at CouchDB and it would find any change handlers in any of the design documents and keep them running, consuming changes, and removing or replacing them when the design document changed.
The best part is that it’s written in node.js, which means you can also make HTTP connections out to the rest of the world and put data back in to CouchDB. And since it’s a service that is intended to stay up indefinitely with only one instance, you can also use setInterval to take care of tasks you would have previously put in cron
All the code is over on github.
To start:
node service http://localhost:5984
To define a listener in your design document.
{ ....
"changes":"var sys = require('sys');
var listener = function (change) {
sys.puts(JSON.stringify(change));
}
exports.listener = listener;",
}










I don’t get it
Why is this useful?
When would I want to do something like this?
Cheers,
Nick
You can do **anything* based on data changes, including operations that require IO.
You can pull in a new users twitter stream, send email notifications, anything!
And the application is still fairly portable since the code is in the design document
-Mikeal
I see the usefulness part now – that is pretty cool.
Have you done anything like this yet where one could see it being used in a practical sense as opposed to theory?
well, i finished this today, so i’m not using it in production yet. But I do already have other code that sits on a single database waiting for changes in order to trigger the same kinds of operations, it’s just not using this system and the code for the listener isn’t in the ddoc.