Ratchet client message

780 Views Asked by At

I have already integrated pusher using Ratchet. Broadcasting to all users works fine.

Now I'm trying to find a way how to send a message to specific user when I got it's connection.

method which is executed on subscribe :

public function onSubscribe(ConnectionInterface $conn, $topic) {
    $conn->send(json_encode("Hello"));
}

JS on client side:

var conn = new ab.Session('ws://127.0.0.1:8080',
                function() {
                    conn.subscribe('chat', function(topic, data) {
                        console.log(data); // here I'd like to get that "Hello" message
                    });
                },
                function() {
                    console.warn('WebSocket connection closed');
                },
                {'skipSubprotocolCheck': true}
        );

I'm not getting any message, I guess I haven't formatted it properly (json_encode("Hello")). Any help ?

1

There are 1 best solutions below

2
On

You need to store the connections somehow and then call them directly.

For instance:

$conns[$conn->resourceId] = $conn;

And then later:

$conns[$resourceId]->write("new data");

As for why you're not receiving the message on subscribe, try the write() method instead of the send() method.

Also, check the console to see if you're receiving the data but not unpacking it properly.