views:

24

answers:

2

Problem Statement - Would like to know if particular web app user is active (i.e. logged in and using site) and be able to query for list of active users or determine a user's activity status.

Constraints - Doesn't need to be exact (i.e. if a user was active within a certain timeframe, that's ok to say that they're active even if they've closed their browser).

I feel like there should be a design pattern for this type of problem but haven't been able to find anything here or elsewhere on the web.

Approaches I'm considering:

  1. Maintain a table that is updated any time a user performs an action (or some subset of actions). Would then query for users that have performed an action within some threshold of time.

  2. Try to monitor session information and maintain a table that lists logged in users and times out after a certain period of time.

  3. Some other more standard way of doing this?

How would you approach this problem (again, from a design pattern perspective)?

Thanks!

A: 

I think #1 is adequate solution for your problem and I guess also common one. It lets you decide what HTTP requests you consider a user "being active" (i.e. to decide when and whether to update the "last activity" table) and/or classify the activities (having more "last XXX activity" columns and update each of them after a user performs different action).

doom2.wad
+1  A: 

You'll have a lot more luck maintaining it if you think of it in terms of a component or module rather than a table. The way I see it, you need the following interfaces.

public interface IActiveUsers {
  bool IsUserActive(User u);
}
public interface ITrackActiveUsers {
  void MarkUserAsActive(User u);
}

You could implement these using a database, but why not just use your chosen web framework's built-in cache?

Basically When marking the user as active insert a key into the Cache with a key like "LoggedInUser-1234" where 1234 is some unique identifier for the user. Then simply set the timeout for whatever you want (5 minutes, 10, etc.) and the value to true. Then, when querying if the user is active just check whether the cache contains a true value for that same key.

George Mauer