Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

AJAX is a superb technology but it has some sharp edges. Here are couple of things that I came across when developing AJAX enabled applications. Let's say that you have a page which sends request to the server using ASP.NET 2.0 Client Callbacks (Behind the scenes the client callbacks calls the XmlHttp object but in a different way as the request is still processed on the server side). The request is send using a HTML input button control.

protected void Page_Load(object sender, EventArgs e)

{

// register callbacks

RegisterCallbacks();

}

private void RegisterCallbacks()

{

string script = String.Empty;

string cbRef = ClientScript.GetCallbackEventReference(this, "arg", "callback", String.Empty);

if (!ClientScript.IsClientScriptBlockRegistered("CallServer"))

{

script = "function makeServerCall(arg,context) {" + cbRef + "}";

// inject the script

ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", script, true);

}

}

public string GetCallbackResult()

{

System.Threading.Thread.Sleep(5000);   // only for demonstration purposes

return _argument;

}

public void RaiseCallbackEvent(string eventArgument)

{

_argument = eventArgument;

}

 

Here is the button code that actually makes the request:

<input type="button" value="Call Server" onclick="callServer()" />

function

callServer()

{

makeServerCall('Hello World','');

}

function

callback(response)

{

alert(response);

}

Everything works fine until the user got disconnected from the internet or the application server goes down. Now, the user presses the input button and nothing happens. No data is returned since the data is assigned at the server side and since there is no connection or server is down then there is no data returned. In this case the callback function is also not fired.

The question is how will you notify the user that nothing happened? How will you tell the user that "Hey! the user interface is dead!". I guess the answer is to notify the user that something has happened instead of notifying that something has not happened. The scenario that I discussed might be too vague for this example but consider a "School Testing Application" where the students are taking an online exam. They have 50-100 questions (multiple choice) and they select the answer. As, soon as the answer is selected the choice is saved in the database and a message is displayed "Successfully Saved!". If the message is not displayed then it means that there is some problem. You can also reload the page after every 5 minutes to get the fresh copy out of the database. This will put an extra protection of loosing unsaved answers. On the other hand if the user clicks two answers very quickly then there is a possibility that only one of them got saved. I have discussed this problem below:

Another common problem with using AJAX enabled applications is making two AJAX requests one after the other. This will cancel the first request and the later request will be made. In the code above I am purposely making the thread to sleep for 5 seconds now, if during that time another request comes then the old request will be killed. Dino Esposito also discussed this problem in his talk in DNR and he concluded that this is an architecture problem of ASP.NET framework.

What problems did you faced when developing an AJAX application and how did you solved it?

Posted on Thursday, November 1, 2007 10:42 AM | Back to top


Comments on this post: AJAX Application and Client Connectivity

# re: AJAX Application and Client Connectivity
Requesting Gravatar...
Hi Azam,

Nice blog entry. I agree, I think at best the ASP.Ajax architechture is slow & clumsy, and has some fundamental issues.

When I was using ASP.Ajax, I discovered that a simple round-trip on a timer object (checking for updates) had anywhere between a 50-100KB payload. At the end of the day I ended up changing most of my stuff over to be more javascript/JSON driven, and only leaving in ASP.Ajax where it was absolutely unavoidable.

Using JSON took the round-trip payload down to about 14bytes, so nice & quick. It also allowed me to perform my own error handling on the client side, as you can put in delegates for unsuccessful calls to the server.
Left by Andrew on Nov 01, 2007 10:53 AM

# re: AJAX Application and Client Connectivity
Requesting Gravatar...
Hi Andrew,

Thanks for the comments. Actually, I was talking more in general and not only the ASP.NET AJAX framework. Most of the AJAX frameworks (I think I should use Ajax with lower case :)) which communicate with the server have the trouble to letting the client know if they are not able to connect to the server at all.

You are also correct about MS ASP.NET AJAX as being slow and sluggish. That is because of huge JS library and the page life cycle.

Hey! what libraries are you using for JSON calls as I know creating JSON objects by hand is a pain.
Left by Mohammad Azam on Nov 01, 2007 3:58 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net