bobsov534
bobsov534

Reputation: 243

Using socket.io to replace polling mechanism against Mongodb

I have a application that I am working on that uses Mongodb and Nodejs. There is another external web services which collects data and updates Mongodb. I would like to have a way to publish those changes automatically in web page without doing actual polling. Currently, I am using polling mechanism within socket.io to push the changes to UI. Is it possible to refresh UI with auto publishing the changes from Mongodb using socket.io?

Upvotes: 0

Views: 1002

Answers (1)

Angular University
Angular University

Reputation: 43117

So there is no control over the web service, it's an external application that uploads data to mongo.

MongoDB provides a special type of polling for new data, which does not cover the case where existing data is modified.

This optimized polling on new data can be made by combining MongoDB’s capped collections and tailable cursors features. Here is a blog post of how to do it, and a code example.

So the way to implement what you mention is via polling, and there is this optimization available for the case of new data only.

If there was access to the uploading web service:

For a solution without polling using server socket.io push, you would need to make the upload web service run in the same node process, and from the callback that saves in Mongo call io.sockets.emit('yourChannel', newData);, to broadcast the new data to all browser clients.

Upvotes: 2

Related Questions