I integrated Websocket in my wordpress website(in a plugin) and when i launch it all other requests are blocked since the $server->run method is a loop. Any suggestion to fix it please ?
<?php
namespace Phenix\API\WebSocket;
use Ratchet\App;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class WebSocket_Server implements MessageComponentInterface
{
public function onOpen(ConnectionInterface $conn)
{
}
public function onMessage(ConnectionInterface $from, $msg)
{
}
public function onClose(ConnectionInterface $conn)
{
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
try {
$server = new App("192.168.1.121", 3000, "192.168.1.121",); $server->route('/ws', new WebSocket_Server(), ['*']);
} catch (\Exception $e) {
error_log($e->getMessage() . PHP_EOL, 3, DEBUG_LOGS_FILE);
}
4
u/liamsorsby Dec 26 '24 edited Dec 26 '24
Your issue is with the run method blocking the request. You will need to expose this as a seperate process.
I'd create a new service to create and manage the websocket server using an entirely different process / worker.
2
u/MateusAzevedo Dec 26 '24
The websocket server should run as its own process, completely separated from the web server/requests.
In other words, you need to run it as PHP CLI,
php myfile.php.