Reputation: 13355
How do you implement a real-time update like Twitter does ("423 more tweets since you started searching")? I need to refresh my page at least every 10 seconds. I'm using ASP.NET MVC and jquery.
Upvotes: 1
Views: 1029
Reputation: 1039418
You could use the setInterval javascript function which allows you to poll the server for updates at regular intervals using AJAX:
window.setInterval(function() {
// This will be executed each 5s
// TODO: query your server for updates probably using AJAX
// example with jquery:
$.getJSON('/arethereanyupdates', function(data) {
if (data.isupdates) {
alert('yeap there are updates');
}
});
}, 5000);
There's also a push technology in HTML5 called WebSockets which allows the server to notify the client for updates but of course you will need an HTML5 compatible browser which nowadays is not difficult to find and a WebSocket API compliant server.
Upvotes: 5
Reputation: 16377
In the absence of long-polling (aka COMET) support in todays browsers (this should, however, change soon, with HTML5), I'm polling the server every n seconds using AJAX request and sending the timestep of the latest visible entry/item/tweet/whatever. Then the backend count the number of newer items and returns it back to the browser.
Upvotes: 0