'websocket with looped content may require asynchronous
I'm trying to create a chat solution in php that will allow for messages to be receive via a websocket but I've run into the issue where client one that is connected will hog the main process due to the code for checking for new messages being a loop (I'm using rabbitmq and listening for new messages on the queue).
Is there a php construct that can be used to run code that is essentially a loop asynchronously with the main process.
The main process will be a script that hosts a websocket solution to clients with the aim of getting messages from clients but also forwarding incoming messages to clients. Rabbitmq handles messages via queue construct and are consumed like below, where the intention is $ec->process($user, $content) sends a message of to the client
Adding the below code to the connected function blocks any further comms to/from the client so the hope is to move it onto a thread or have it run asynchronously
From what I can tell most libraries that cater for async functions will wait for a return or complete event, in the case of the below the application is not concerned with a response though its aim is just to rely any new messages on the queue to the user..
I have tried using spatie but it feels like the aim is the response going back, it never looked like the code was being executed.
Would something like amphp be able to accommodate for such scenario, can someone recommend something else, my php version is 7.2 I'm not convinced that threading is the way to go if it will be depricated in future and comes with possible performance issues,
If anyone has experience or other suggestions regarding chat solutions that are PHP based I am not set on using anything specific, the solution doesnt need to cater for many users.
//
$channel->queue_declare('hello2', false, false, false, false);
$ec = $this->echo;
$us = $this->user;
$callback = function ($msg) use ($ec, $us){
$message_data = explode("::", $msg->body);
$user_id = $message_data[0];
$message = $message_data[1];
$content = '[x] Received ' . $message . " from " . $user_id;
//$this->send($user,$content);
$ec->process($us, $content);
}
//$channel->basic_consume('hello2', '', false, true, false, false, $callback);
I'm using this as the backend websocket solution https://github.com/ghedipunk/PHP-Websockets
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
