Passing data between different PHP instances

475 Views Asked by At

index.php is parsed by Apache. It needs to pass some data to another PHP instance which happens to be a reactphp loop. How can this be best accomplished? Both scripts are run on the same machine.

<?php
//index.php
$status=sendDataToOtherInstance('hello'); //blocking function which returns true/false

.

<?php

//reactphp loop

$foo->on('connection', function ($data){
    echo($data);
    return $data==='hello';
});
3

There are 3 best solutions below

0
On BEST ANSWER

Few options:

  1. HTTP between the 2 processes

    Like you say this is a bit messy, not too bad, it will be quick, but a little messy.

  2. Unix socket file

    Only problem is reactphp doesn't work with unix sockets at the minute.

    More info: https://github.com/reactphp/socket/pull/17

  3. TCP Socket

    This should work. But just as clunky as the 1st option.

To be honest it sounds like your getting to the limits of php.

Node.js

I'm not sure if you've ever used it, but node.js was made for this sort of task. And allows lots of functionality php does not.

https://nodejs.org/en/

0
On

You need some sort of inter-process communication.

You can do it with IPC (of course); using shared memory. Or with slightly worse performances, but IMHO better manageability, using a temporary file on a RAM disk or temporary file system. Of course IPC also gives you the ability of waiting on semaphored resources, which may come handy and improve responsiveness (you could have the same by opening a socket, or a full HTTP server, in the reactphp process).

You could also do this by leveraging external applications (e.g. some *MQ, or Redis).

If you have a database such as MySQL, you can have the two processes "communicate" through a shared table.

0
On

As LSemi said you can do it using shared memory or basically you already got a WebSocket server Why don't you get responses with PHP WebSocket Client library ?

https://github.com/Textalk/websocket-php