views:

24

answers:

1

Hi,

I am developing a feature in my app where I need to store the state of a particular series of actions between 2 users, just like an ajax based chat service.

The scenario is as follows: A user can is able to see which other users are online and then challenge one of them. The other user receives the challenge and accepts it. Now both the users are given 5 questions each and the match starts simultaneously for them (almost simultaneously) Then as a user navigates thru the question or solves it, the state is updated on the other user screen as well.

Essentially this is very similar to a chat system like say facebook chat. I am able to see who all are online. I send a chat msg to my friend who can then respond to that and that response is seen on my chat window.

I believe all this can be achieved by using ajax. I can easily make ajax calls to .asmx webservices and retrieve objects for a particular user as Session can be accessed there. However, I am wondering where to maintain state because session is for a particular user and I want my particular object to be accessible for two particular users.

Where do I store the state? Or taking the example of Ajax based chat, where should I store what message User1 entered and when Uer1 entered how is it shown to user2?

I was thinking application object but read it is not recmmended.

What do you recommend for such a thing?

+1  A: 

If you're trying to do "near real-time" message passing you might want to take a look at HTTP polling (a.k.a. long polling). I won't use SQL for temporal message passing and short-term state transitions like I've seen in the past. If running on a single web server just keep the state in session or in the ASP.NET Cache. If running on multiple web servers take a look at distributed caches like memcached, Velocity (Win 2008), or NCache. Then serve the cached data to the AJAX requests which are sitting and waiting (because of long-polling). The key design issue is the design of the cache keys (no pun intended), which would need to include the user's ID for user-specific event data.

P.S. There are frameworks for large-scale, near real-time message distribution that solve scaling issues which are hit when hundreds of clients are participating in long-polling at the same time. The broad name for those frameworks are 'Comet', and they are most useful when broadcasting the same messages to many clients.

crtracy
@crtracy: Good points. I've deleted my answer (since you had a reference to it).
Daniel Vassallo
thanks for those pointers... I will read up and try those and post my results here soon..
sassyboy
A question regarding storing the state in Cache. What if the server starts running out of memory.Will the cache not destroy the objects to free up memory, in which case another incoming request may throw an error? Is this a possibility and what can i do in that case?
sassyboy
Using the cache to store the messages would work for a prototype and saves you from having to write the 'message expiration' logic for managing the lifetime of the messages. In a 'real' application you'll want to manage the lifetimes (push them into a queue, and pop the expired ones off the other end of the queue). http://msdn.microsoft.com/en-us/library/dd267265.aspx
crtracy