在数据库端实现Comet
这更多是出于好奇和“以供将来参考”,但 Comet 在数据库端是如何实现的呢? 我知道大多数实现都使用长期 HTTP 请求来“等待”直到数据可用,但这是如何在服务器端完成的? Web 服务器如何知道新数据何时可用? 它会不断轮询数据库吗?
This is more out of curiosity and "for future reference" than anything, but how is Comet implemented on the database-side? I know most implementations use long-lived HTTP requests to "wait" until data is available, but how is this done on the server-side? How does the web server know when new data is available? Does it constantly poll the database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你使用什么数据库? 如果它支持触发器(许多 RDBMS 以某种形式执行此操作),那么您可以让触发器触发一个事件,该事件实际上告诉 HTTP 请求发送适当的响应。
触发器消除了轮询的需要...轮询通常不是最好的主意。
PostgreSQL 似乎有很好的支持(甚至 PL/Python)。
What DB are you using? If it supports triggers, which many RDBMSs do in some shape or form, then you could have the trigger fire an event that actually tells the HTTP request to send out the appropriate response.
Triggers remove the need to poll... polling is generally not the best idea.
PostgreSQL seems to have pretty good support (even PL/Python).
这在很大程度上取决于应用程序。 最可能的实现是某种消息传递系统。
最有可能的是,您的服务器端代码将由相当多的部分组成:
设施最后一点,消息传递基础设施确实是关键。 这为应用程序服务器提供了一种与 comet 服务器通信的方式。 因此,当请求传入时,应用程序服务器会将一条消息放入消息队列中,告诉 Comet 服务器通知正确的客户端。
同样,消息传递的实现方式很大程度上依赖于应用程序。 一个非常简单的实现只需使用名为
messages
的数据库表并对其进行轮询。但根据您计划使用的堆栈,应该有更复杂的工具可用。
在 Rails 中,我使用 Juggernaut 它只是监听某个网络端口。 每当有数据要发送时,Rails 应用程序服务器都会打开与这个强大的推送服务器的连接,并告诉它要发送给客户端的内容。
this is very much application dependent. The most likely implementation is some sort of messaging system.
Most likely, your server side code will consist of quite a few parts:
the last one, the messaging infrastructure is really the key. This provides a way for the app servers to talk to the comet server. So when a request comes in the app server will put a message into the message queue telling the comet server to notify the correct client(s)
How messaging is implemented is, again, very much application dependent. A very simple implementation would just use a database table called
messages
and poll that.But depending on the stack you plan on using there should be more sphisticated tools available.
In Rails I'm using Juggernaut which simply listens on some network port. Whenever there is data to send the Rails Application server opens a connection to this juggernaut push server and tells it what to send to the clients.