如何获得有关帆帆mongo集合中外部变化的通知? [英] How to get notified about external changes in a mongo collection with sails?

查看:75
本文介绍了如何获得有关帆帆mongo集合中外部变化的通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发基于mongo和sails的应用程序,并且正在测试Sails中的实时更新的工作原理.

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

我现在使用的是Sails 0.9.16,但我也对有关Sails 0.10的答案感兴趣.

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

我希望在相应的集合中创建新文档时更新列表.当我通过Sails套接字添加文档并发送发布消息时,此方法有效.在这种情况下,我看到其他客户端收到消息,并且客户端上的列表已更新.

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.

在mongo数据库上很难编写一个外部服务,因此该集合一直在增长.外部服务直接在数据库中创建的新元素不会通知正在监听的客户端,因此我必须刷新网页才能显示这些元素.

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.

问题:

  • 当数据库创建不是来自帆本身时,有关数据库创建的通知是否应该起作用?
  • 如果是,这需要一些特殊的配置吗?
  • 如果不是,那么在数据库更改时,如何使客户端有关集合的清单更新的推荐方法是什么?

欢呼

推荐答案

一个非常有趣的问题,尽管不是一个不寻常的问题:

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.

因此,起初,他们通过每10秒轮询一次数据库来对它进行修补. :)显然,这不是最佳解决方案,因此他们最终提出了另一个解决方案(也可以用于Sails):现在,他们正在跟踪MongoDB

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.

这篇关于如何获得有关帆帆mongo集合中外部变化的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆