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):
I have also looked into the following:
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 :-)
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!
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 :-).
Long polling is more resource intensive on the server than a WebSocket connection. Long polling can come with a latency overhead because it requires several hops between servers and devices.
To give you the “TL;DR” answer: yes, websockets are faster than Ajax, but you will still need Ajax sometimes.” My answer doesn't even take Andrea's response into account— if the actual implementation has less overhead, that's also great!
AJAX long polling Using AJAX long polling will mean, that the client sends a request to the server and the server waits for new data to be available before he responds. This would look like this: The client sends a request and the server responds "irregularly".
Send a request to the server, keep the connection open, get an answer when there's "data" for you. This will cost you only one request (per user), but the request keeps a permanent connection between client and server up.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With