在数据库端实现Comet

发布于 2024-07-22 21:12:28 字数 129 浏览 4 评论 0原文

这更多是出于好奇和“以供将来参考”,但 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏闹i 2024-07-29 21:12:28

你使用什么数据库? 如果它支持触发器(许多 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).

染年凉城似染瑾 2024-07-29 21:12:28

这在很大程度上取决于应用程序。 最可能的实现是某种消息传递系统。

最有可能的是,您的服务器端代码将由相当多的部分组成:

  • 一些处理传入请求的应用程序服务器,
  • 一个处理与客户端的所有开放连接的(单独的)comet 服务器,
  • 数据库以及
  • 某种消息传递基础

设施最后一点,消息传递基础设施确实是关键。 这为应用程序服务器提供了一种与 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:

  • a few app servers that hansle incoming requests,
  • a (separate) comet server that deals with all the open connections to clients,
  • the database, and
  • some sort of messaging infrastructure

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文