in reply to Re^4: Creating Socket Connections that are persistent across multiple page loads in a single web session
in thread Creating Socket Connections that are persistent across multiple page loads in a single web session

Yes. You cannot serialize sockets. So you need a persistent program on your server that keeps the socket open. That persistent program gives the client ("web browser") a token ("a session id", "a cookie") that associates the socket with the client. When some client connects again to your server process and presents the appropriate token, you know which connection to reuse.

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

Replies are listed 'Best First'.
Re^6: Creating Socket Connections that are persistent across multiple page loads in a single web session
by unlinker (Monk) on Jan 05, 2011 at 09:57 UTC
    OK! Let me give implementing this a shot. Thank you for your patience :)

      I have done this using the POE framework. I keep a persistent connection to two tcp servers and marshal all CGI connections through them, keeping track using unique Ids and using JSON for passing the data between them.

      use POE qw(Component::Schedule Filter::Reference Component::Server::TCP Component::Client::TCP Filter::Line Component::Log4perl Component::Daemon );

        Thanks for the suggestion. Have not used POE before (but have read a bit about it in Simon Cozens book "Advanced Perl Programming") so will pass it for now, in favour of a simple Dancer function that hands out sockets to requestors. This function keeps track of which socket belongs to which requestor using a hash whose keys are session ids. If the session id is a key in the hash it just hands out the corresponding socket, if not it creates a new one, records it in the hash and hands it out. Since I run the Dancer app under Starman, I believe the handed sockets would be persistent.

        Would POE give me a more robust solution than this obviously simplistic method? If yes, I would love to give it a go.