Websocket or Long Polling (using AJAX) to detect Changes in Database using PHP Websocket or Long Polling (using AJAX) to detect Changes in Database using PHP ajax ajax

Websocket or Long Polling (using AJAX) to detect Changes in Database using PHP


Here's the best comparison I've seen regarding long polling and the 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).

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.

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.

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.


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

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.