Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring Mongo for changes with Node.js

I'm using Node.js for some project work and I would like to monitor my Mongo database (collection) for changes, basically fire an event if something gets added.

Anyone know if this is possible? I'm using the node-mongodb-native drivers.

If it's not I'd also like any available pointers on pushing data from the server (run with node) to the client browser.

like image 248
Josh K Avatar asked Jun 21 '10 20:06

Josh K


People also ask

Is MongoDB good for node JS?

Node and MongoDB work very-well together, in part because Mongo uses a JavaScript engine built into the database since JavaScript is good at handling JSON objects. Compared to other databases, such as MySQL, MongoDB is fast for storing certain types of data and can be automatically scaled.

Why use MongoDB why is it so often used with node js?

MongoDB represents the data as a collection of documents rather than tables related by foreign keys. This makes it possible for the varied types of data dealt over the internet to be stored decently and accessed in the web applications using Node. js.


2 Answers

The question is if all data is added to your database through your node.js app. If so, you can use the EventEmitter class of node.js to trigger an event (http://nodejs.org/api.html#eventemitter-14).

If the database is populated by some other app, things are getting difficult. In this case you would need something like a database trigger, which is AFAIK not yet availabled in MongoDB.

Pushing Events to the Client (aka Comet) will be possible once the HTML 5 websockets API makes its way into all major browsers.

In the meantime, you can only try to emulate this behaviour using techniques like (long-term) AJAX polling, forever frame etc. but each of them has its weaknesses.

like image 77
fbuchinger Avatar answered Sep 23 '22 02:09

fbuchinger


I would turn on replication in your mongodb. There is a replicate? database that contains a list of changes, similar to the mysql replication log. You can monitor that.

-daniel

like image 33
Daniel Avatar answered Sep 23 '22 02:09

Daniel