Blog Posts

Thursday, April 22, 2010

Chating in asp.net using Ajax

In this post I want to share my work on developing Chatting application that can be used in your regular ASP.Net applications. I am using AJAX and SQL server for backend. This can be used in ASP.Net 2.0 or more, as I am using the UpdatePanel. The application works as follows -
  • List of online users are displayed.
  • The current logged-in user will click on a selected to user name to open a new chat window, and types a message.
  • Once the message is sent, on the selected user's screen a new pop-up window will appear.
  • The users can communicate through the current windows.
  • A single user can communicate with multiple users at a time.


When the First user sends a message, the message is saved to backend for the selected user. When the application running on the recipient (implemented in default.aspx in sample) uses the Ajax to ping the server for messages for each 5 seconds.

function PingServer() {

PingServerForMsg();

serTimeout(PingServer, 5000);

}

.....

var requestURL = 'GetChatMsg.aspx';

xmlHttp = GetXmlHttpObject(stateChangeHanlder);

xmlHttp_Get(xmlHttp, url);

........

On response, process the response and show the message in pop-up if any messages found.

function stateChangeHandler() {

...

//if message found open chat window - see the attachment for the complete code

OpenChatBox(msg[0], msg[1]);

...

}


When a message is found at server a new pop-up appears on the recipient screen.

Visit http://www.codeproject.com/KB/aspnet/Chating_in_asp_net_Ajax.aspx to download source code.

No comments:

Post a Comment