Amirhossein Mehrvarzi
Amirhossein Mehrvarzi

Reputation: 18944

Pass Info to Client without Page Refresh in Asp.net MVC Application

Today we have seen some sites that pass data or notification to client without page refresh. Named Real time or interactive applications.

some of known site are :

  1. Stackoverflow : notifications
  2. Freelancer : passes project and professional counts asynchronously in numeric format
  3. Google Mail : Counts mail memory usage by users in total.

and so more ... .

I have tried and searched some tools like SignalR. Basically SignalR designed for creating chat application. But is there a direct way without any extension in Microsoft Technologies to meet our purpose? For example suppose we want a simple counter like freelancer, Have we no way except using extensions like SignalR?

Upvotes: 1

Views: 2733

Answers (3)

Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 15138

You can look at a technique called polling (which SignalR falls back to when support for other methods are not present), basically the concept is that every x seconds you'd send a request to the server to check for an update (more or less), for example (using jQuery):

setInterval(function() {
    $.get("/Messages/GetCount", function(data) {
        // do something with the data ...
    });
}, 30000);

Every 30 seconds, check the Messages count - and perform an action accordingly. Here is a good article on polling and long polling (it mentions a SignalR alternative called Socket.IO).

Having said all that, I'd seriously just go with SignalR, those guys tested all kinds of corner cases, performance etc.

Upvotes: 3

Yaakov Ellis
Yaakov Ellis

Reputation: 41480

Use a Javascript timer on the client-side to make periodic asynchronous requests for updated information. This updated information can then be used to update the client-side, or can be used to prompt further requests for more details.

This solution can work for situations where you do not need to receive immediate updates whenever there are updates available on the server side (but instead can wait for the timer interval). It also may present some scaling issues and can lead to wasting bandwidth and client/server time while making unnecessary calls.

To overcome either of these, it would be best to use a library like SignalR (which can do much more than just chat applications - check out this blog post for a real world implementation that has nothing to do with chat).

Upvotes: 1

leanne
leanne

Reputation: 8719

Use Microsoft's ASP.NET Ajax implementation or JQuery:

Microsoft Ajax Overview

Upvotes: 0

Related Questions