tags:

views:

265

answers:

3

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?

A: 

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.

Wayne
You can definitely use Persistent HTTP Connection. So I don't think it's impossible.
Pablo Santa Cruz
Thanks for the -1. What you says work but I think ASP.NET is limited to 20 simultaneous PHTTP connections. I think you can raise to a 100. Would need to research this.
Wayne
A: 

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.

Anwar Chandra
A: 

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

Pablo Santa Cruz