apache - read/write websocket server in php is not wotking
one text
I am running an apache server on a linux machine. Im trying to create a website which has a page that needs asynchronous updates and two-way communication with the server.
Im trying to make this simple file which fires up as an intermediate server for each session the code looks something like this:
<?php
$server = stream_socket_server("tcp://localhost:8090", $errno, $errstr);
if(!$server){
echo "$errstr ($errno)<br>";
die();
}
$users = [];
//var_dump($server);
while(true){
$client = stream_socket_accept($server); // new connection
echo "new connection<br>";
array_push($users, $client);
$pid = pcntl_fork();
if($pid){
while(true){
$buff='';
while(!str_ends_with($buff, "\n\r")){
$buff = $buff . fread($client, 1024);
}
//$len = strlen($buff);
foreach($users as $u){ // broadcast
fwrite($u, $buff);
}
if($buff == 'END'){
exit(1);
}
echo "routed<br>";
}
}
}
?>
This is supposed to be a basic echo broadcast. it keeps track of new connections and waits for any client to send a message and sends it back to everyone.
the basic idea was that it would listen for a new connection and use fork to handle the read and write processes.
However I've recently learned that fork() does not work on apache and so my question is how can i make this work.
Source