Can I peek at all "active sessions"?

My users need to login to run the app I’m building. I’d like to give
them an “Other users currently active:” piece of information. I could
track it myself in the database based on login/logout, but that doesn’t
really count for people just leaving the computer logged in and never
coming back. Does Rails have an API for me to monitor sessions and take
a peek at their info?

I’m googling as best I can but I have to sift through all the references
to developer conferences with “ruby sessions” :slight_smile:

D

Duane MOrin wrote:

My users need to login to run the app I’m building. I’d like to give
them an “Other users currently active:” piece of information. I could
track it myself in the database based on login/logout, but that doesn’t
really count for people just leaving the computer logged in and never
coming back. Does Rails have an API for me to monitor sessions and take
a peek at their info?

That can be done (see my post about “SessionCleanup” – a look at the
software will give you a good idea how to go about it).

BUT – it’s not any more useful than tracking logins and logouts.
Sessions can’t tell you when somebody wanders away from a computer and
doesn’t come back.

Probably the best approach would be to keep a database record of people
presently logged in with a “last_activity_time” field, and update it
whenever they performed some activity. Then you could present a list of
everyone who had done something in the past ten minutes, for example.

–Al Evans

On 6/22/06, Al Evans [email protected] wrote:

Probably the best approach would be to keep a database record of people
presently logged in with a “last_activity_time” field, and update it
whenever they performed some activity. Then you could present a list of
everyone who had done something in the past ten minutes, for example.

This would work pretty easily. Add a column to the sessions table,
then a before_filter in application.rb to update the field. This
wouldn’t work if your users viewed only cached pages for X consecutive
minutes, but it should be close enough (if that’s even an issue).

Pat

Sure, here’s how. Create a file called dump_sessions (no .rb extension)
in your /script subdirectory. Paste the following code:

#!/usr/bin/env ruby
require ‘pp’
require File.dirname(FILE) + ‘/…/config/environment’
Dir[‘app/models/**/rb’].each{|f| require f}
pp Dir[’./tmp/sessions/ruby_sess
’].collect {|file| [file,
Marshal.load(File.read(file))] }

Now, from the root of your app, type ruby script/dump_sessions and there
you have it. :slight_smile:

David

If you use DB Sessions you can add a before filter in application.rb to
set
some session variable, make sure you have a DB column updated_at, and
then
just query that field.

adam