How to do live table updates with HTML5 SSE?
How to do live table updates with HTML5 SSE?
tisawyer
Posts: 12Questions: 4Answers: 1
I have a php script which produces json like so:
summary: [{"area_name":"Area 51","device_name":"001 - Test Device 001","device_no":"40","sensor_name":"psBatteryVoltage1","sensor_value":"13.5"},{"area_name":"Area 51","device_name":"001 - Test Device 001","device_no":"40","sensor_name":"psBatteryVoltage2","sensor_value":"13.32"},{"area_name":"Area 51","device_name":"001 - Test Device 001","device_no":"40","sensor_name":"psBatteryVoltage3","sensor_value":"13.45"},{"area_name":"Area 51","device_name":"001 - Test Device 001","device_no":"40","sensor_name":"psOutputCurrent","sensor_value":"2.8"}]
I'm attempting to process it with this snippet.
// Start SSE
var source=new EventSource("db/sse.summary.php?area=" + area + "&type=" + type);
// Fires when data comes in.
source.addEventListener('summary', function(event) {
console.log('summary: ' + event.data);
//var summary = JSON.parse(event.data);
//$('#countAll').html(summary.length)
$('#summary').bootstrapTable({
ajax: event.data,
columns: [
{'data': 'device_name'},
{'data': 'sensor_name'},
{'data': 'sensor_value'}
]
});
});
The are no javascript errors. The event is firing and I can print the data to the console. What I don't know how to do is pull the data from the SSE listener and get it into my table's columns. Thanks for helping.
Tim
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Looks like you might want to use
data
to tell DataTables what data to display (ajax
tells it where to get the data, but you've already got it...). So you might usedata: event.data.summary
.Allan
I got it to work by doing 2 things. First was to properly handle a flat array data source as documented https://datatables.net/examples/ajax/custom_data_flat.html
Then I had to change my php code by removing the forever loop. What I want to do is this typical SSE php. But dataTables didn't like the nonstop data and the SSE format.
So the question remains. Is there a way to continuously update a dataTable with Server Sent Events?
Yes, but you need to handle the SSE part yourself. Then use API, such as
row.add()
,row().data()
androw().remove()
to update the DataTables with the incoming data.Allan
I've decided to use
rows.add()
to update a DataTable from SSE data. But DataTables gives a warning.However the array of objects (below) looks Ok to me. What am I missing?
sseData from php is an array of objects like so:
php code above post has not changed.
I figured out what I was missing. I needed to use
columns.data
like so: