Websocket或Long Polling(使用AJAX)使用PHP检测数据库中的更改

时间:2022-09-04 08:38:59

My Research

I have spent hours researching all over Google and SO about Websockets and Long Polling, their pros and cons etc but am yet to find a clear solution to this.

我花了几个小时研究Google和SO,关于Websockets和Long Polling,它们的优点和缺点等,但我还没有找到一个明确的解决方案。

I have read more articles on this topic then anything I have ever researched, including these (to name a few):

我已经阅读了更多关于这个主题的文章然后我研究过的任何内容,包括这些(仅举几例):

  1. Ajax/PHP - should I use one long running script or polling?
  2. Ajax / PHP - 我应该使用一个长时间运行的脚本还是轮询?
  3. Long Polling using jQuery and PHP
  4. 使用jQuery和PHP进行长轮询
  5. Poll Database for Changes - Ajax/jQuery
  6. 轮询数据库的变化 - Ajax / jQuery

I have also looked into the following:

我还研究了以下内容:

  1. http://cometdproject.dojotoolkit.org/
  2. http://cometdproject.dojotoolkit.org/
  3. http://socket.io/
  4. http://socket.io/

Justification for this Question

At first glance, this question may seem like a duplicate or too localised, however, after my extensive research I was unable to gather enough information to make an informed decision of which route to go down.

乍一看,这个问题可能看起来像是重复或过于局部化,但是,经过我的广泛研究后,我无法收集足够的信息来做出明智的决定,确定哪条路线会失效。

I am therefore hoping that one of you SO geniuses will kindly lend your time to answer this question and share your brilliant knowledge with the rest of us :-)

因此,我希望你们中的一位天才会帮助你们回答这个问题并与我们其他人分享你们的精彩知识:-)

My Question

In short, my question is really in the title; If I am trying to detect changes to a database record, is it better to use a websocket (socket.io) or long polling (jQuery and AJAX)?

简而言之,我的问题确实在标题中;如果我试图检测数据库记录的更改,使用websocket(socket.io)或长轮询(jQuery和AJAX)是否更好?

If the answer is websockets, then please include a basic example as these have really confused me, even with all of the articles on Google...

如果答案是websockets,那么请包括一个基本的例子,因为这些让我很困惑,即使是Google上的所有文章......

Furthermore, there may be other ways of doing this which are better or more suited, if so please share them, I am open to any suggestions!

此外,可能有其他方式做到这一点更好或更适合,如果是这样请分享,我愿意接受任何建议!


Additional Information

This will probably not affect the final answer, but just in case, I would like to explain what I am trying to detect and a few things that may need to be considered.

这可能不会影响最终答案,但为了以防万一,我想解释一下我想要检测的内容以及可能需要考虑的一些事项。

Effectively, I am trying to detect any changes to a login session. In other words, if a users token has changed in the database or their timeout has run out, then they have been logged out and I would like to display a message informing them of this.

实际上,我正在尝试检测登录会话的任何更改。换句话说,如果用户令牌在数据库中已更改或其超时已用完,则它们已被注销,我想显示一条消息,通知他们这一点。

I don't think this will make a difference, but the final code for this will need to be suitable for SSL. This is easy with AJAX, but I am unfamiliar with the websockets side.

我不认为这会有所作为,但最终的代码需要适合SSL。使用AJAX很容易,但我不熟悉websockets方面。

Originally, I wrote a system that retrieved the timeout from the server using javascript, and then in timeout seconds, it would poll the server to see if the timeout had expired. I thought this was perfect, until I realised that it only worked when the time on the clients computer was matched to the time on the server. I therefore had to scrap this :-(

最初,我编写了一个系统,使用javascript从服务器检索超时,然后在超时秒内,它将轮询服务器以查看超时是否已过期。我认为这是完美的,直到我意识到它只有在客户端计算机上的时间与服务器上的时间相匹配时才有效。因此我不得不废弃这个:-(

Anyway, I hope my question is not too localised and I look forward to hearing your views and answers. Please don't waste your time helping me with the PHP database code, or the jQuery AJAX code, unless there are complex parts to it, as I am capable of writing this part myself and there are plenty of others on the Stack that need your help more than me. I am more interested in your opinions, and/or how to achieve this with websockets if they are the better solution :-).

无论如何,我希望我的问题不是太局部化,我期待听到你的意见和答案。请不要浪费你的时间来帮助我使用PHP数据库代码或jQuery AJAX代码,除非它有复杂的部分,因为我能够自己编写这个部分,并且堆栈上有很多其他需要你的帮助比我多。如果他们是更好的解决方案,我对你的意见和/或如何使用websockets更感兴趣:-)。

2 个解决方案

#1


1  

Here's the best comparison I've seen regarding long polling and the WebSocket API:
http://www.websocket.org/quantum.html

这是我在长轮询和WebSocket API上看到的最佳比较:http://www.websocket.org/quantum.html

As the above article states, the WebSocket API is far superior to long polling (or any other pseudo-bidirectional communication), but the one downside is that browser support still isn't quite there (IE finally started supporting the WebSocket API in IE10).

正如上面的文章所述,WebSocket API远远优于长轮询(或任何其他伪双向通信),但一个缺点是浏览器支持仍然不存在(IE终于开始支持IE10中的WebSocket API) 。

As such, if you're looking for a full-on bidirectional communication solution, use the WebSocket API when it's available and fall back to Ajax long polling or whatever comet method you prefer when it's not.

因此,如果您正在寻找全面的双向通信解决方案,请在可用时使用WebSocket API并回退到Ajax长轮询或任何彗星方法。

To answer your questions, if you're only making an occasional server-side/DB query (with the term "occasional" being relative and subject to testing on your system), then simple Ajax requests should be fine. However, if you're hammering the server with requests every 10 seconds or less, then using the WebSocket API is definitely ideal.

要回答您的问题,如果您只是偶尔进行服务器端/数据库查询(术语“偶尔”是相对的并且需要在您的系统上进行测试),那么简单的Ajax请求应该没问题。但是,如果您每10秒或更短时间使用请求锤击服务器,那么使用WebSocket API绝对是理想的选择。

To answer your last question, SSL is 100% available with the WebSocket API. Simply use the wss protocol instead of the standard ws protocol, and you're there.

要回答您的上一个问题,使用WebSocket API可以100%使用SSL。只需使用wss协议而不是标准的ws协议,你就在那里。

#2


0  

You wrote in part "... I am trying to detect changes to a database record ...". There's nothing in websockets designed to detect a db change; that's just not what it's for.)

你写的部分是“......我试图检测数据库记录的变化......”。用于检测数据库更改的websockets中没有任何内容;这不是它的用途。)

It appears to me that an economical implementation has that change agent (logout in your case) notifying listeners at the time of change, rather than monitoring somehow to detect that change.

在我看来,一个经济实现的变更代理(在您的情况下注销)在更改时通知侦听器,而不是以某种方式监视以检测该更改。

I mean that the db was changed by some specific script actions. I'd look at extending each of those actions to also trigger a websocket transaction.

我的意思是db已被某些特定的脚本操作更改。我会考虑扩展每个动作以触发websocket事务。

#1


1  

Here's the best comparison I've seen regarding long polling and the WebSocket API:
http://www.websocket.org/quantum.html

这是我在长轮询和WebSocket API上看到的最佳比较:http://www.websocket.org/quantum.html

As the above article states, the WebSocket API is far superior to long polling (or any other pseudo-bidirectional communication), but the one downside is that browser support still isn't quite there (IE finally started supporting the WebSocket API in IE10).

正如上面的文章所述,WebSocket API远远优于长轮询(或任何其他伪双向通信),但一个缺点是浏览器支持仍然不存在(IE终于开始支持IE10中的WebSocket API) 。

As such, if you're looking for a full-on bidirectional communication solution, use the WebSocket API when it's available and fall back to Ajax long polling or whatever comet method you prefer when it's not.

因此,如果您正在寻找全面的双向通信解决方案,请在可用时使用WebSocket API并回退到Ajax长轮询或任何彗星方法。

To answer your questions, if you're only making an occasional server-side/DB query (with the term "occasional" being relative and subject to testing on your system), then simple Ajax requests should be fine. However, if you're hammering the server with requests every 10 seconds or less, then using the WebSocket API is definitely ideal.

要回答您的问题,如果您只是偶尔进行服务器端/数据库查询(术语“偶尔”是相对的并且需要在您的系统上进行测试),那么简单的Ajax请求应该没问题。但是,如果您每10秒或更短时间使用请求锤击服务器,那么使用WebSocket API绝对是理想的选择。

To answer your last question, SSL is 100% available with the WebSocket API. Simply use the wss protocol instead of the standard ws protocol, and you're there.

要回答您的上一个问题,使用WebSocket API可以100%使用SSL。只需使用wss协议而不是标准的ws协议,你就在那里。

#2


0  

You wrote in part "... I am trying to detect changes to a database record ...". There's nothing in websockets designed to detect a db change; that's just not what it's for.)

你写的部分是“......我试图检测数据库记录的变化......”。用于检测数据库更改的websockets中没有任何内容;这不是它的用途。)

It appears to me that an economical implementation has that change agent (logout in your case) notifying listeners at the time of change, rather than monitoring somehow to detect that change.

在我看来,一个经济实现的变更代理(在您的情况下注销)在更改时通知侦听器,而不是以某种方式监视以检测该更改。

I mean that the db was changed by some specific script actions. I'd look at extending each of those actions to also trigger a websocket transaction.

我的意思是db已被某些特定的脚本操作更改。我会考虑扩展每个动作以触发websocket事务。