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):
- Ajax/PHP - should I use one long running script or polling?
- Long Polling using jQuery and PHP
- Poll Database for Changes - Ajax/jQuery
I have also looked into the following:
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 :-).