i am working on a chat application where the chat message from one client gets into the database and the other clients poll every 5 secs (using a timer) whether there is any new message or not, if yes, the messages are fetched and displayed. But this creates a problem when one user is on a slower and connection and the other one is on a very fast one. I want to develop a mechanism where a method gets executed just when a new row is inserted in the database so that the need for polling does not remain. The users will fetch the msgs only when a new chat message enters into the database by any of the clients, otherwise all remain idle. Any help would be highly appreciated. I am using asp.net and C#.
-
1The answer to this depends a lot on the architecture of the application. Could you be more specific about this. I see one of the tags specified is ajax-polling, is this a browser based asp.net application? or is it windows clients all communicating with some form of wcf service? – jdavies Aug 05 '11 at 08:53
2 Answers
What you are describing is moving from a polling model to a publish/subscribe model (which as you can appreciate is a much better way to do chats/feeds than polling). Two possible resources are this article Publish/Subscribe design pattern implementation in C# and this previous stack overflow question about Pub/Sub architecture.
I'll stay away from commenting on different methods of implementing your application and concentrate on answering the question you asked - you can accomplish this using Query Notifications. There is a lot you can do with these, but the basic premise is that once you have set this up, you give SQL server a query you are interested in, and it will notify your application when the results change.
There's a reasonable tutorial (the code is in VB.NET, but it doesn't really change much) here.

- 29,854
- 11
- 77
- 120