How to get notified about external changes in a mongo collection with sails?

a 夏天 提交于 2020-01-06 02:17:05

问题


I am developing an application based on mongo and sails, and i am testing how the realtime update in sails works.

I am using sails 0.9.16 now, but i am interested also in answers about sails 0.10.

I want a list to be updated when new documents are created in the corresponding collection. This works when i add documents via sails sockets, sending a post message. In that case i see other clients receiving a message and the list on the client side is updated.

There is an external service writing on the mongo database tough, so the collection is growing all the times. The new elements created directly by the external service in the database are not notified to listening clients, so i have to refresh the web page in order to show those elements.

Questions:

  • are notifications about database creations supposed to work, when those creations do not come from sails itself?
  • if yes, does this require some special configuration?
  • if not, what would be a recommended way to keep client side listing about a collection updated when the database is changing?

Cheers


回答1:


Very interesting question, though not an unusual one: the guys from Meteor were having the same problem. Basically, without watching the DB you can't even scale your app horizontally, since one server process will have no idea on what data changes were made by another one.

So, at first they sort of patched it by polling the DB every 10 seconds. :) Obviously, it wasn't the best solution, so they ended up with another one (which can also work for Sails): now they are tailing the MongoDB oplog and fire an update whenever there's a change in the corresponding collection.

That said, to answer your questions:

  • AFAIK, a Sails process has no clue about any external changes made to the DB;
  • so, nothing to configure;
  • a way to track external DB (MongoDB) updates would be using one of the oplog watchers you can find in npm (e.g. this or one of these, etc.) to listen to the changes and trigger updates whenever there's a need.

Unfortunately, no ready-to-use solution here, but I hope at least that now you have an idea on how to make it work.



来源:https://stackoverflow.com/questions/23083913/how-to-get-notified-about-external-changes-in-a-mongo-collection-with-sails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!