new React\Socket\Server($loop); $http = new \React\Http\Server($socket); $counter = 0; $http->on( 'request', function (\React\Http\Request $request, \React\Http\Response $response) use (&$counter, &$loop) { $counter++; echo "Handling request number $counter" . PHP_EOL; $headers = ['Content-Type' => 'text/html']; $response->writeHead(200, $headers); $process = new \React\ChildProcess\Process('fortune'); $process->start($loop); $process->stdout->on( 'data', function ($output) use (&$response) { $response->write($output); } ); $process->on( 'exit', function ($rc, $signal) use (&$response) { $response->end(); } ); } ); echo "Socket server listening on port 4000.\n"; echo "You can connect to it by running: telnet localhost 4000\n"; $socket->listen(4000); $loop->run();