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?
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.
No. I don't think the server can send message to client's browser. Here is how I implement chat application:
- client1 post message via Ajax to server
- server save it to repository (I'm using singleton object for this case)
- client2 get the message from repository
- mark the message as read
I will save chat logs to database once the chat session closed or expired.
Best thing you can do is use a Persistent HTTP Connection. The way google does with Google Talk on their GMAIL website.