I have a backend NodeJS code where I gather output from a spawned process:
schild.stdout.on('data', function (data) {
var result = data.toString().split("\n");
for(var i = 0; i < result.length; i++)
{
var n = result[i].split("|");
if(n.length == 4)
{
var date = n[0];
var category = n[1];
var component = n[2];
var condition = n[3];
response.push({Date : date, Category : category, Component : component, Condition : condition});
}
}
logger.info('Rendering : ' + response.length);
res.render('viewpage', {status:0, msg:'Success', responsedata:response});
});
My viewpage.ejs file has below code:
var oTable = $('#example').dataTable({"bDestroy" : true, "bUseRendered": false, "bSort" : false, "bPaginate" : true, "bDeferRender": true, "iDisplayLength" : 27});
var status = <%= status %>;
if(status == 2)
{
alert( '<%= msg %>' );
}
if(status == 0)
{
<% for(var i =0; i < responsedata.length; i++){ %>
$('#example').dataTable().fnAddData(['<%= responsedata[i].Date %>', '<%= responsedata[i].Category %>' , '<%= responsedata[i].Component %>', '<%= responsedata[i].Condition %>']);
<% } %>
oTable.fnDraw();
}
} );
The issue is that the page gets rendered only for the first call to 'res.render' but I wish to update the page with further output as captured in 'schild.stdout.on' event. I do see within my logger statements that first time the response array is of 0 length which is correct but subsequently I get valid data and render to the view - but the page does not gets updated with latest data. The page only loads once and no further updates occurs on the page?
So you'll need a different design to make this work. Something like socket.io can help with streaming data from the server to the browser, but you'll want to do it along the lines of:
data events into socket.io messages as a single unit of data is avalable