Search code examples
phpwordpressiiswebsocketreactphp

ReactPHP blocking process in wordpress plugin


i am trying to make a websocket server in my wordpress plugin.

when i try to access the site it loads alot of time and outputs this iis error:

The FastCGI process exceeded configured request timeout

websocket server code:

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server('127.0.0.1:9000', $loop);

$socket->on('connection', function (ConnectionInterface $conn) {
    $conn->write("Hello " . $conn->getRemoteAddress() . "!\n");

    $conn->on('data', function ($data) use ($conn) {
        $conn->close();
    });
});

$loop->run();

when i remove this code ^ it load fast and no error.

versions:

PHP: 7.2
ReactPHP: ^1.0 
Wordpress: 4.9.2

Solution

  • Hey ReactPHP core maintainer here. Running any socket server including a websocket server is intended to run from the CLI or using supervisor/systemd. My suggestion would be to create such a special cli command in your plugin that you can call to start it. Doing funky magic with XHR requests is very error prone and not very stable. Alternatively if you absolutely cannot run cli commands is a service like pusher