Prabhu
Prabhu

Reputation: 13355

Real-time updates like Twitter

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

Answers (3)

Darin Dimitrov
Darin Dimitrov

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

Tomasz Zieliński
Tomasz Zieliński

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

SubniC
SubniC

Reputation: 10367

You can do an AJAX call to your server asking for updates, if the answer is positive (there are new things) you can show a javascript windows advising the user or just update the page content you need.

regards.

UPDATE: This how-to implements similar behavior

Upvotes: 1

Related Questions