PHP persistent UNIX sockets between sessions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cjhegarty
    New Member
    • Jul 2007
    • 2

    PHP persistent UNIX sockets between sessions

    Hello,

    I have an application which runs on a single linux box, with a C++ backend server and a web-based front end, with apache also running on the local server.

    I'm using PHP to communicate with the C++ backend, and would like to just connect to the C++ server once, and have bilateral communication as follows:
    1. As the user carries out actions on the web front end, just use fwrite to communicate with the backend on the persistent socket.
    2. Have a small javascript routing using timeout to regularly call a php routine to process any (asynchronous) messages the server might send us on the socket. This is the primary reason I want the socket to stay connected.

    I should point out that the volume of traffic is pretty low - this is a "control" application for some hardware, but we're talking a few kbytes an hour.

    The problem I'm encountering is that I cannot find a way to pass a socket object between one call to a PHP script and another. The PHP sessions paradigm works great for passing integers and strings for example, but doesn't work for passing a socket object. I can understand why that would be the case, but does anyone have any hints as to how I might achieve what I'm trying to do?

    I like PHP and I am very familiar with sockets, so that's why I'm trying to do it this way, but I'm not particularly wedded to either if someone has a suggestion for a better approach. I *DO* need to use a browser front end for other reasons.

    Any help much appreciated.

    Chris Hegarty
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Chris. Welcome to TSDN!

    Have a look at pfsockopen().

    Comment

    • cjhegarty
      New Member
      • Jul 2007
      • 2

      #3
      Originally posted by pbmods
      Heya, Chris. Welcome to TSDN!

      Have a look at pfsockopen().
      Hi,

      Thanks!

      Actually I am already using persistent sockets, the actual call I'm using is:
      Code:
      stream_socket_client("unix:///tmp/hs.socket",$errno,$errstr,2.0,
                                 STREAM_CLIENT_CONNECT|
                                 STREAM_CLIENT_PERSISTENT);
      Maybe I need to explain a bit what I'm doing - it may well be that I have a conceptual problem in my code which someone can identify.

      I call the PHP script from javascript using xajax calls (see http://www.xajaxprojec t.org/). I only have a single PHP script with all of the functions I call from javascript in it, but obviously each call to a PHP function like the following:

      Code:
      function InitialSetup()
      {
      ....
      xajax_OpenSocket();
      ....
      }
      runs the PHP script once, which then exits.

      I have been trying to save the stream resource returned by stream_socket_c lient() in the $_SESSION array (which works just great for atomics like integers and strings ) but it does not seem to be able to save the PHP stream resource. So when I call the PHP script again, I no longer have the stream resource to access the socket (in other words, I assume it is still open, I just don't have any way to get to it). I also tried passing the PHP stream resource reference back up to javascript using xajax but that didn't work either (or at least I couldn't find a way).

      Does anyone have a simple solution for this? I must be missing something obvious...

      Thanks again

      Chris

      Comment

      • bucabay
        New Member
        • Apr 2007
        • 18

        #4
        I know this is an old thread.

        What you need to do is forget about saving the variable returned from:

        stream_socket_c lient("unix:///tmp/hs.socket",$err no,$errstr,2.0,
        STREAM_CLIENT_C ONNECT|
        STREAM_CLIENT_P ERSISTENT);

        The stream_socket_c lient() is persisted when you make an identical call, from the same process.

        So you can just use it normally, knowing that if the stream_socket_c lient() already has an equivalent socket resource, it will return that resource instead of creating a new one.

        What to watch though is that you may not land on the same php process for each page load, and thus stream_socket_c lient() will not return the socket resource you created in the last page load.

        Comment

        Working...