At what point are WebSockets less efficient than Polling?

The whole point of a websocket connection is that you don’t ever have to ping the app for changes. Instead, the client just connects once and then the server can just directly send the client changes whenever they are available. The client never has to ask. The server just sends data when it’s available. For … Read more

What is wrong with polling?

Polling is not “wrong” as such. A lot depends on how it is implemented and for what purpose. If you really care about immedatly notification of a change, it is very efficient. Your code sits in tight loop, constantly polling (asking) a resource whether it has changed / updated. This means you are notified as … Read more

ElasticSearch updates are not immediate, how do you wait for ElasticSearch to finish updating it’s index?

As of version 5.0.0, elasticsearch has an option: ?refresh=wait_for on the Index, Update, Delete, and Bulk api’s. This way, the request won’t receive a response until the result is visible in ElasticSearch. (Yay!) See https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-refresh.html for more information. edit: It seems that this functionality is already part of the latest Python elasticsearch api: https://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch.Elasticsearch.index Change … Read more

Socket connections and Polling. Which is a better solution in terms of battery life?

Keeping an idle TCP socket connection open (with no data being sent or received) will not (or at least, should not) consume any more battery than having it closed. That is because an idle TCP connection uses no bandwidth or CPU cycles(*). That said, keeping a TCP connection open for extended periods may not be … Read more

jQuery AJAX polling for JSON response, handling based on AJAX result or JSON content

You could use a simple timeout to recursively call ajax_request. success: function(xhr_data) { console.log(xhr_data); if (xhr_data.status == ‘pending’) { setTimeout(function() { ajax_request(); }, 15000); // wait 15 seconds than call ajax request again } else { success(xhr_data); } } Stick a counter check around that line and you’ve got a max number of polls. if … Read more

What’s the difference between polling and pulling?

They’re two distinct words. To “poll” is to ask for an answer. To “pull” is to use force to move (actually or conceptually) something towards oneself (again, actually or conceptually). One “polls” a server when software on a client periodically asks the server for something. One “pulls” data from a database towards client software. Note … Read more

How to wait for a WebSocket’s readyState to change

This is simple and it work perfectly… you can add condition about maximal time, or number of try to make it more robust… function sendMessage(msg){ // Wait until the state of the socket is not ready and send the message when it is… waitForSocketConnection(ws, function(){ console.log(“message sent!!!”); ws.send(msg); }); } // Make the function wait … Read more

Using setInterval() to do simplistic continuous polling

From my comment: I would use setTimeout [docs] and always call it when the previous response was received. This way you avoid possible congestion or function stacking or whatever you want to call it, in case a request/response takes longer than your interval. So something like this: function refresh() { // make Ajax call here, … Read more