Tutorial :Chat Application in ASP.NET



Question:

I've to write an Ajax chat web application in ASP.NET for a friend, and I've a question: if client1 sends a message to client2, how should the application send the message to client2? Is there a better way than sending requests to the server, "asking" if there are new messages? Is it possible to directly send the message to the client?


Solution:1

Best thing you can do is use a Persistent HTTP Connection. The way google does with Google Talk on their GMAIL website.


Solution:2

Remember that HTTP is a stateless protocol and that each transaction is made from the client to the server.

The server can use sessions to determine if this client is "known" but as for sending information back to the client using plain old HTTP I think that is impossible (I mean from a server initiated connection, not a response to the client)

You would need to use Javascript to poll the server for information.

If you want it the other way around, you could possibly use Java or Flash but then you also need to think about NAT tunneling, proxy servers and any other weird setups that the clients could be using.


Solution:3

No. I don't think the server can send message to client's browser. Here is how I implement chat application:

  1. client1 post message via Ajax to server
  2. server save it to repository (I'm using singleton object for this case)
  3. client2 get the message from repository
  4. mark the message as read

I will save chat logs to database once the chat session closed or expired.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »