I want to perform a long running task in ratchet , so I did:
public function onMessage(ConnectionInterface $from, $msg)
{
// if message is from this server,broadcast message, and end it there
echo "on message $msg";
sleep(1000);
}
to simulate a long running task.
I have a php script that uses pawl library to send a message to ratchet.
However, that script runs fine the first time, but second time onwards , it waits.
Why is this synchronous ?
I had expected to receive some message from client , and performing an HTTP request internally.
The http request will be a fairly long running task (few hundred ms). Is what Im looking for not possible ?
sleep is a synchronous command. It blocks.
That is just for simulating a long running task. I will probably perform something that will also block.
Other questions in this repo point out that child-process library may be used. is that any good?
my code is:
$loop = \React\EventLoop\Factory::create();
$webSock = new \React\Socket\Server($loop);
$webSock->listen($this->port, $this->host);
$webServer = new \Ratchet\Server\IoServer(
new \Ratchet\Http\HttpServer(
new \Ratchet\WebSocket\WsServer(
new PusherServer()
)
),
$webSock
);
return $loop;
now, I want my onMessage() to always be free as there might be a few blocking tasks going on (one is the aforementioned http request, and another is Push notification) .
How can I integrate that library ?
@cboden , any help? :/
I am using ratchet inside laravel , and whenever a message is received , I want to send a push notification (GCM/APNS) , to all clients. Sending to all is a fairly time consuming task, and it will be blocking.
what should I do?
Yup, if you need to do blocking tasks the ChildProcess library is good for that. The main process will communicate in async to any child processes and the child processes will have blocking code in them that will not impact their parent.
any code for ratchet that I can see ? I haven't used react and I'm not familiar with the technology so well
Check out this blog post from @WyriHaximus on using ChildProcess.
Most helpful comment
Check out this blog post from @WyriHaximus on using ChildProcess.