M
M
MasterCopipaster2021-02-23 23:11:41
PHP
MasterCopipaster, 2021-02-23 23:11:41

Any idea how to dispatch events in WS ratchet server?

Hello everyone, I wrote a small WS server according to this article
As a result, I got the following console command
Which starts the WS server itself

<?php
    namespace App\Command;
     
    use Ratchet\Http\HttpServer;
    use Ratchet\Server\IoServer;
    use Ratchet\WebSocket\WsServer;
    use App\Websocket\MessageHandler;
    use Symfony\Component\Console\Command\Command;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
     
    class WebsocketServerCommand extends Command
    {
        protected static $defaultName = "run:websocket-server";
     
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            $port = 3001;
            $output->writeln("Starting server on port " . $port);
            $server = IoServer::factory(
                new HttpServer(
                    new WsServer(
                        new MessageHandler()
                    )
                ),
                $port
            );
            $server->run();
            return 0;
        }
    }


And a connection handler
<?php
    namespace App\Websocket;
     
    use Exception;
    use Ratchet\ConnectionInterface;
    use Ratchet\MessageComponentInterface;
     
     
    class MessageHandler 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)
        {
     
        }
    }


Actually, the question is, I have a bunch of clients connected to WS, and they don’t communicate with each other, they only receive messages, I need to send 1 message to all connected clients.
That is, I need to somehow pull the onMessage events in order to send messages to all connected clients.
( Well, probably ... I think so ) - but even here I don’t know how to properly pull such an event? I still need to check if I have a new message for them, I thought to put the message in memcache and take it from there, but at what point and where should I check if the message appeared there?
In general, who worked with ratchet, can you tell me how, for example, to check memcache for a new message? or somehow dispatch the event of sending a message... I hope I formulated the question clearly...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rPman, 2021-02-24
@MasterCopipaster

Connect to your own web server and send it a system message (do not forget about authorization).
The websocket ratchet server is a 1-threaded application; accordingly, after receiving the message, the method will be executed in the same space where you can store the list of clients. I remember exactly that the ratchet had an array with a list of all connected clients, I'm not sure if it is publicly available directly, but in the worst case, you can collect your own list, available globally.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question