11
votes

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.

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. Long Polling using jQuery and PHP
  3. Poll Database for Changes - Ajax/jQuery

I have also looked into the following:

  1. http://cometdproject.dojotoolkit.org/
  2. 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)?

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

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.

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 :-(

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

2
Not exactly an answer but I got really good results using pubnub.com It's easy to implement and quite fast.Chris
you can considering using redis database so you can fire events in case data changes and make a live bidirectional connection with the client to extend needed data with socket.ioeyurdakul
@Chris Thank you for your input, but I don't really want to pay for something I can do myselfBen Carey
@eyurdakul Afraid Redis is not an option, this application links into an SQL Database, which also links into a Desktop Application, so I cannot move to a different database engine :-(Ben Carey
About your timeout problem in bold: Use UTC time instead of client time to achieve this.Salketer

2 Answers

1
votes

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.

0
votes

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.