Your application wants to show live data i.e. data sent from Server back up to the Client instead of the usual which is the Client sending data to the Server via a form submit.
There are multiple options, currently the best option is WebSockets.
The Client sends some information to the Server and wants the Server to send back a response, the response is not immediate so the Client wants to wait for the Server but instead of waiting the Client keeps sending requests to the Server and when something is updated on the Server then the Client updates the UI.
JavaScript accesses a browser object EventSource to manage this.
https://www.w3schools.com/html/html5_serversentevents.asp
There are multiple options, currently the best option is WebSockets.
Polling
Periodically check the Server for updated data, uses SetInterval in Javascript.The Client sends some information to the Server and wants the Server to send back a response, the response is not immediate so the Client wants to wait for the Server but instead of waiting the Client keeps sending requests to the Server and when something is updated on the Server then the Client updates the UI.
(function poll(){
setTimeout(function(){
$.ajax({ url: "server", success: function(data){
//Update your dashboard gauge
salesGauge.setValue(data.value);
//Setup the next poll recursively
poll();
}, dataType: "json"});
}, 30000);
})();
https://davidwalsh.name/javascript-pollingLong-Polling
Similar to Polling but without the periodic checks, instead the Server holds onto the Client side request and when some data is ready to be sent it then responds to the request.(function poll(){
$.ajax({ url: "server", success: function(data){
//Update your dashboard gauge
salesGauge.setValue(data.value);
}, dataType: "json", complete: poll, timeout: 30000 });
})();
COMET
Ajax Push, Forever Frame.
The Server sends data back to the Client without the Client requesting it.
HTML5 Server Sent Events
The Server sends data to the Client, the Client watches particular Events and does something with the data in a handler.JavaScript accesses a browser object EventSource to manage this.
https://www.w3schools.com/html/html5_serversentevents.asp
WebSockets
A connection between Client and Server which stays open and communication is in both directions.
https://www.html5rocks.com/en/tutorials/websockets/basics
Comments