Can Ratchet WebSocket Server send a message to client itself? Can Ratchet WebSocket Server send a message to client itself? php php

Can Ratchet WebSocket Server send a message to client itself?


That is indeed possible. You need to communicate with the WebSocket server process somehow. You can do that by using some form of message passing, be it RPC or a message queue.

Ratchet itself is based on the React event loop. This means that any form of communication with Ratchet must be integrated with that event loop. On the React homepage you can see some of the integrations that already exist:

In the Ratchet documentation there is a tutorial on how to use React/ZMQ in order to push messages from anywhere to your WebSocket server.


Ratchet also implements WAMP, which includes PubSub. So your clients can subscribe to some topics, and you can have other clients (running i.e. on your backend infrastructure) publish to those topics. You could i.e. have an AutobahnPython based client publishing via Ratchet to an AutobahnAndroid based mobile app or AutobahnJS based HTML5 client.


I had the exact same question and here is what I did.

Based on the hello world tutorial, I have substituted SplObjectStorage with an array. Before presenting my modifications, I'd like to comment that if you followed through that tutorial and understood it, the only thing that prevented you from arriving at this solution yourself is probably not knowing what SplObjectStorage is.

class Chat implements MessageComponentInterface {    protected $clients;    public function __construct() {        $this->clients = array();    }    public function onOpen(ConnectionInterface $conn) {        // Store the new connection to send messages to later        $this->clients[$conn->resourceId] = $conn;        echo "New connection! ({$conn->resourceId})\n";    }    public function onMessage(ConnectionInterface $from, $msg) {        $numRecv = count($this->clients) - 1;        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"            , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');        foreach ($this->clients as $key => $client) {            if ($from !== $client) {                // The sender is not the receiver, send to each client connected                $client->send($msg);            }        }        // Send a message to a known resourceId (in this example the sender)        $client = $this->clients[$from->resourceId];        $client->send("Message successfully sent to $numRecv users.");    }    public function onClose(ConnectionInterface $conn) {        // The connection is closed, remove it, as we can no longer send it messages        unset($this->clients[$conn->resourceId]);        echo "Connection {$conn->resourceId} has disconnected\n";    }    public function onError(ConnectionInterface $conn, \Exception $e) {        echo "An error has occurred: {$e->getMessage()}\n";        $conn->close();    }}

Of course to make it really useful you may also want to add in a DB connection, and store/retrieve those resourceIds.