http://www.perlmonks.org?node_id=880524

unlinker has asked for the wisdom of the Perl Monks concerning the following question:

I have asked this in a few places but did not get any answers and I am hoping to find understanding from the wise monks here.

I have a perl web application (CGI::Application with ModPerl::Registry) which connects to a authenticated custom server over a socket and exchanges data (command/response) with it. Currently the web application connects to the server, authenticates and disconnects on every page request - even for the same user.

Is there some way I can use the same socket over multiple page requests which share a common session id? Creating a separate daemon that proxies connections and makes them persistent, is an option I am exploring, but would like to know if there are any simpler solutions. I have no control over the design of the custom server unfortunately, so modifying that is not currently an option. However moving to another web framework like Dancer, if that supports what I want to achieve, is definitely an option

Thank you.

  • Comment on Creating Socket Connections that are persistent across multiple page loads in a single web session

Replies are listed 'Best First'.
Re: Creating Socket Connections that are persistent across multiple page loads in a single web session
by Anonymous Monk on Jan 05, 2011 at 07:44 UTC
    Is there some way I can use the same socket over multiple page requests which share a common session id?

    There is only way and its very simple, after your program arranges with the remote server for a persistent connection, remain connected, don't disconnect, keep connection alive, and keep using it.

      How do I pass the socket from one pass through the web page to the next?

        You don't. You keep the socket open in the server and give the client a session id (see CGI::Session) using which you can find the appropriate socket back in your server program.

Re: Creating Socket Connections that are persistent across multiple page loads in a single web session
by Anonymous Monk on Jan 05, 2011 at 20:54 UTC
    #!/usr/bin/perl -w use strict; package MyConnections; use vars ($dbh); unless($dbh) { # do stuff here, establish connection/etc } 1; Then simply include a module as similar to the above if you're already using Apache mod_perl/registry. The module remains persistent (compiled) per each HTTPD process Apache spawns, thus any program which uses the above module would have access to the already instantiated $dbh. We use this method for memcached and database handles - the only caveat being that you have to manage one connection per httpd process on the server but it works very well.

      Thank you for your advice. I have recently moved away from mod_perl to relying on Plack-based frameworks. The problem I am trying to solve is having a single socket per authenticated-session rather than one per httpd process.

      However the principal part of your suggestion (if I have understood you correctly): keep the socket creation code in a separate module that is different from the code that handles the web request is clearly the way to go in any persistent web framework. So thanks for that.