I'm a somewhat new web designer and developer working on a public health project to estimate the course of HIV/AIDS in a population in different countries. Our test version is here: http://globalhealthdecisions.com/test/tool/
We built this epidemic model in Ruby. The model calculates the different probabilities of sexual interaction and disease transmission between groups for each year. Currently, it runs through ~40 years and at the end, outputs the final results as a JSON object. The script takes ~8 seconds to complete running it in the CLI. I've implemented a front-end for the model, and with the AJAX request, it can take up to 30 seconds to receive a response from the server.
My question: is there a way for me to receive an update from the server as the model is being calculated? Ie, could I calculate one year, output that year, have it sent to the client, while still running the current execution of the Ruby script? If so, in this "push" manner, I could load each year as it is received, decreasing wait times significantly.
Thanks so much.
Assuming that you can get the chunks of data you need from your script I think you may be looking for some sort of polling, long polling perhaps. Here's a good article on the topic:
http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery
Here's the gist of it using jquery, from the above article:
(function poll(){
$.ajax({ url: "server", success: function(data){
//Update your dashboard gauge
salesGauge.setValue(data.value);
}, dataType: "json", complete: poll, timeout: 30000 });
})();
Essentially it's just a loop of ajax requests that calls the success function every time the server completes a request, timing out completely, in this case, after 30000ms.
Hope that helps.