Dancer Sessions using PSGI

A few weeks on #dancer channel, a user was having issues using Dancer::Session::PSGI. I've never worked directly with Plack besides reading the handbook and few play apps. (well a few weeks ago, I dug into Dancer::Debug.). Using both together was an intriguing problem stuck in my mind.

The person on the channel was reporting unreliable reading of session data and other odd behavior. As I started to dig into the problem, I realized that i need to create two apps, one pure Plack and one Dancer with middleware wrapper. I created a public repo on github with my test apps: Dancer and Plack session.

Since, I didn't want to deal with html and wanted a bit of structure with return data, I made both test apps return JSON and have pretty simple routes

I started off using the documentation from Plack::Middleware::Session to create this test app:

Next I created a Test Dancer PSGI app, which basically had a way to show value and update it. Here is the non-exciting Dancer app:

I updated bin/ to use Plack::Builder directly instead of creating a wrapper (which I'd like to try as well probably in a branch):

With those all setup, I started both apps using plackup but each with slightly different command lines:

 magic-bus> plackup pure_plack/bin/app.psgi &
 magic-bus> plackup -p 3000 dancer_plack_session/bin/ &

I created a curl_cookie script to use and store cookies and verbose dump out request/response information. I needed to see what cookies where getting sent in and back.

After i fired up each app server, the first set of requests dumped the following:

Well that is odd. A couple quick bullets:

  • plack app did what was expected (the app increments counter in session after return response therefore on 2nd request being set to 1 is ok).
  • dancer app didn't return '2' (since other app has write to the counter twice) plus it appears to be using both 'dancer.session' and 'plack_session'. This is wrong.
After I dug around a bit (since that session name seems wrong), I found two things.
  • A bug with Dancer::Session::Abstract: #1004
  • And in this case, Dancer::Session is only creating the session not retrieving (since it doesn't think there is one yet) which makes sense. (why retrieve when you are first generating the session.)

The initial session being created to the first request to Dancer not finding the existing plack session information. After that initial request, then it would pick up plack session data. The current behavior would lead to this confusing response the 2nd time from Dancer app:

   "visits" : 2

I fixed up Dancer::Session::Abstract to use 'plack_session' cookie and that resolved the session not getting read on initial request.

A bit unexpectedly, I see that Dancer is setting cookie with different session id than PSGI. PSGI includes its own session cookie. This is expected since the Plack middleware runs after Dancer (think of the onion from handbook: ).

Here is example of response made to Dancer app:

In the end its working now. Dancer is able to pull in session information from Plack session but its still a bit messy. I'm not sure if multiple session cookies is considered a bug or not. I think it might be best to have Dancer not do any cookie stuff and only use the session data if its available in the environment but looking over the Dancer session code, this doesn't look straightforward w/out major changes to core Dancer code.

Any ideas out there?



  1. Chase4Net IT Solutions
    Best Website Design company - PHP,JAVA,ANDROID App Development Company

    Chase4Net is a Software Company and having office at Chennai & bangalore . Our main focus areas are Web Development and Software Development.
    Chase4Net has been ranked as the No.1 for the web designing company in Chennai & Bangalore.

    How to start the Online Business ?

    To start the Business in online is very simple and more profitable. Today starting the Website designing Company is one of the more profitable business with less investment.

    We are having 8 years’ Experience in web design business , we will guide to start the software company quickly with less effort.

    Now a days all the business are moving towards online . Too start the company we need correct Business Model and that is Chase4Net . We will help you to setup the Website design Company at less cost . The website design company can offer the following Logo design, website design , webhosting , domain registration ,android app development .

    Even If you do not know how to develop website no worries !! Chase4Net will be there, always with you , we can help you on the business by developing a website a behalf of you.

    Our company products are Hotel booking website design |, Bus ticket booking website design | , shopping cart website design | , hospital website design |,
    bill software design |,job website design |,tickect booking website design | ,matrimoney website design |.

    By just becoming a reseller (or) By just taking franchise with Chase4Net, We will take you to the next level.

    Enter into partnership with us so that we can help you to setup your business including website design for your company, Email-Id creation, Web Hosting membership , Domain Reseller membership, Payment Gateway Integration, SMS Integration.
    Chase4Net ensures you to start the business from day one rather than making you run behind business plans.

    To be our franchise we charge as low as INR Rs 20000/- (or) $296

    To pay the franchise membership fee follow the link How to Pay Membership fee

    Chase4Net IT Solutions
    (+91) 9884925203 or 4464130523

  2. Great Article. Finally, I’ve found something that helped me. katmoviehd


Post a Comment

Popular posts from this blog

Template Toolkit Debugging inside of Perl Dancer

Changing Dancer::Plugin::Ajax's content type

BootstrapX clickover