Implementing Comet on the database-side

佐手、 提交于 2019-12-08 02:47:26

问题


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?


回答1:


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).




回答2:


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.



来源:https://stackoverflow.com/questions/933029/implementing-comet-on-the-database-side

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