I want to write an Ajax web application, a game to be specific. Two web clients have to communicate with each other via the PHP server. My approach to solve this is to use Ajax between client and server and server and client. Each client creates a separate server process using Ajax. I want that these two server processes communicate via MySQL and via named pipes. I need the named pipes to get immediate response for the whole application.
I cannot use one server process, which first creates a pipe and then forks into two processes, which use the pipe. Web applications create server processes when the web-browser sends a request. So, I need named pipes, where each process doesn't know more than the file name of the named pipe. They cannot exchange file handles (at least I don't know how).
My problem is that named pipes in the PHP way indeed work as long as they are used within the same function:
public function writeAndReadPipe_test(){
$pipeA = fopen("testpipe",'r+');
fwrite($pipeA, 'ABCD');
$pipeB = fopen("testpipe",'r+');
$content = fread($pipeB, 4);
echo "[" . $content . "]<br>
";
}
public function testingPipes_same_function(){
posix_mkfifo("testpipe", 0777);
$this->writeAndReadPipe_test();
}
But, when I use different functions, then the fread($pipeB, 4)
command blocks the whole application:
public function writePipe_test(){
$pipeA = fopen("testpipe",'r+');
fwrite($pipeA, 'ABCD');
}
public function readPipe_test(){
$pipeB = fopen("testpipe",'r+');
$content = fread($pipeB, 4);
echo "[" . $content . "]<br>
";
}
public function testingPipes_different_functions(){
posix_mkfifo("testpipe", 0777);
$this->writePipe_test();
$this->readPipe_test();
}
Does anybody know why? And what can I do to make it work between different functions in the first step? In the second step, it should work even between different processes! I found out that I also get a problem when the writer closes the pipe before the reader reads from it. I suppose that the function closes it automatically when it ends, but this is only a guess.
If the PHP way does not work, I plan to let PHP open a command line, generate BASH commands and let execute them. This should work in any case as long as my web-server works in a LAMP environment. Disadvantage is that it will not work in WAMP environments.
So, has anybody some ideas to this?
P.S: I need blocking pipes to let the reader wait until an event is sent. I know that the pipes can work in a non-blocking mode using
stream_set_blocking($pipe,false);
or so, but the whole idea is to do it without polling just using a pipe, which wakes the reader up as soon as an event is fired.