I am updating a MongoDb database from a streaming application. And I want the changes in the database to reflect in the frontend.
On the frontend, I am currently queering the database, but I have to refresh manually to get new updates. Refreshing periodically is not a good option as the updates can be too quick many times a sec or even very slow a few times an hour.
I came across SSE(server sent events). But don't know if it can be used in my case.
What I was searching for is a good PUSH based method. Where the backend server can change to a frontend client.
What I used: A PULL based method instead. Usually, in such cases, AJAX requests with a setInterval() in javascript can be used. In this case, one doesn't have to refresh the whole page. But just use an ajax request periodically to check for updates at regular intervals. And in case of an update can append/fill the corresponding Html in the concerned .
Here is what I used:
function update(){ // This functions is called repeatedly
$.ajax({
url: "/update/",
type: "POST",
success: function(resp){
$('div.stats').html(resp.data); // append/fill to appropriate div
}
});
}
var myVar = setInterval(update, 1000); // update every 1sec
This is very commonly used to the method. This kind of served my purpose.
Also there are ways to have a PUSH based approach instead. See COMET programming. But I could not find a proper tool to do that in sync with the web frameworks.