11

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

6
  • Not exactly an answer but I got really good results using pubnub.com It's easy to implement and quite fast. Commented Feb 7, 2013 at 12:45
  • 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.io Commented Feb 7, 2013 at 12:49
  • @Chris Thank you for your input, but I don't really want to pay for something I can do myself Commented Feb 7, 2013 at 12:56
  • @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 :-( Commented Feb 7, 2013 at 12:58
  • About your timeout problem in bold: Use UTC time instead of client time to achieve this. Commented Sep 2, 2013 at 7:19

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

That is why I looked into the socket.io, as this will fallback to other methods if the websocket API is not available. The issue I had though was that I could not find a clear explanation of how the socket.io and node.js worked. Despite numerous articles on Google and SO, I could not find anything remotely clear. I am aware of the wss protocol, but not sure how it would work with my installed SSL. Are you aware of any clear explanations of how to detect changes in a database using websockets? Using PHP and Javascript of course?
I agree that the documentation on Socket.IO and the like is scant and not very good, which is why I coded my own PHP WebSocket solution from scratch. The basic principle is that your PHP script is constantly running in a while loop in the background, listening for socket connections. You could easily add DB queries to that script (or a separate included script), and send data to the clients as necessary via the socket connections. Does that answer your questions?
Socket.io even works in ie6 and later versions and all other popular browsers. If html5 websocket don't work then it uses flash
I thought Socket.IO fell back to Ajax long polling first, and if that wasn't there, it used Flash. Maybe I'm wrong.
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.)

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.