Search code examples
perlmojolicious

How can I see all active sessions in a Mojolicious Lite app?


I'm building an app with Mojolicious Lite and I'm looking to give myself a way to watch any and all data about the active sessions. I'm mostly doing this because this is my first foray into using sessions with Mojolicious Lite, and I want to watch what's going on under the hood.

A couple notes: I'm pretty new to Mojolicious, as you might guess by the fact that I'm using Mojolicious Lite. Any Mojolicious Lite apps I've written before have been pretty trivial, so my familiarity with it is not deep. For that matter, I'm still 'early intermediate, at best' with perl, so poking around the inner workings of anything OO in perl is largely foreign territory for me.

That said, I made myself a few little routes like so:

get '/firstpage' => sub{
    my $self = shift;
    my $usr = $self->session(user => 'first_user');
    $self->render(text => $usr);
};

get '/secondpage' => sub{
    my $self = shift;
    my $usr = $self->session(user => 'second_user');
    $self->render(text => $usr);
};

get '/sessions' => sub{
    my $self = shift;
    $self->render(text => Dumper(app->sessions));
};

I'm working off the assumption that, after I visit the first two urls, Mojolicious will have 'some' data somewhere that would confirm that it knows about first_user and second_user. (I could also be totally off base in my understanding of how to use Mojolicious Lite sessions...honestly, from the documentation, I'm not really sure.)

Sadly, /sessions just shows me the contents of the Mojolicious::Sessions object:

$VAR1 = bless( { 'cookie_path' => '/', 'secure' => 0, 'cookie_name' => 'mojolicious', 'default_expiration' => 3600 }, 'Mojolicious::Sessions' );

But I'm assuming that, somewhere, I can get to a hash of all of the session-related data that Mojolicious has. I've been poking around the documentation for a while but I have yet to find any leads.

Any insight?


Solution

  • I'm working off the assumption that, after I visit the first two urls, Mojolicious will have 'some' data somewhere that would confirm that it knows about first_user and second_user. (I could also be totally off base in my understand of how to use Mojolicious Lite sessions...honestly, from the documentation, I'm not really sure.)

    Yeah, I think you're missing the point of sessions. The server/app doesn't remember the state of every user who visits. To allow it to look as if it did, we have cookies. A session is a per-client persistence thing.

    Session information is just a hashreference, encoded as JSON and stored in a cookie on the client side. This is useful for remembering that you are logged in, as what username, perhaps an arrayref of things in your shopping cart. When you request a page, this cookie is sent back to the server, which can access the data and prepare the response for you knowing the state of your session.

    As such there is no record of "active sessions". All that information is distributed amongst all the clients.

    If you would like to get a better idea of what's going on, may I recommend tempire's plugin Mojolicious::Plugin::ConsoleLogger which for the current request shows all of the relevant information (session, stash etc) in your browser's javascript console.

    Here is an example.

    #!/usr/bin/env perl
    
    use Mojolicious::Lite;
    
    #plugin 'ConsoleLogger'; # if desired
    
    any '/' => sub {
      my $self = shift;
      my $name = $self->session('name') || 'Unknown'; # get the name from the session
      $self->render( text => "Hello $name" );
    };
    
    any '/name/:name' => sub {
      my $self = shift;
      my $name = $self->stash('name'); # get name from path
      $self->session( name => $name ); # and store it in the session
      $self->redirect_to('/');
    };
    
    any '/logout' => sub {
      my $self = shift;
      $self->session( expires => 1 );
      $self->redirect_to('/');
    }; 
    
    app->start;
    

    If you visit /name/ghorahn it will store your name in a cookie. From then on, every time you visit / it will say hello to you until:

    1. Your session expires (default 1 hour from your last visit)
    2. You change your name via /name/whatever
    3. You visit /logout to manually expire the session

    You will notice that another user (either on another computer or even a different browser on your same computer) may have a different name, but both are persistent. That is what a session is for. :-)