Quantcast
Channel: Active questions tagged zeromq - Stack Overflow
Viewing all articles
Browse latest Browse all 208

Send user ID from browser to websocket server while opening connection

$
0
0

Before asking this question, I did my best by reading severel questions on SO (tagged Ratchet and dealing with similar issues but to no avail. I even asked a question which received no attention and I therefore deleted it to write another one (that hopefully is more clear).

My final goal is to build a one-to-one private chat application using Ratchet. Everything is working fine except that I can't send message to a specific user.

Every logged in user connects to the websocket server while accessing secured area of website:

$(document).ready(function() {     var conn = new WebSocket('ws://localhost:8080');        conn.onopen = function(e) {            console.log("Connection established!");            // Here I need to send the logged in user_id to websocket server            // and get it in onOpen method so that I can index my array             // of connections with user_id instead of            //$connection->ResourceId, I explain more below        };        conn.onmessage = function(e) {            console.log(e.data);        };});

When a user writes a message in the chat box, the message is sent via AJAX to web server then pushed to Websocket using ZeroMQ. In the controller:

// Persistence of Message(message_id, sender_id, receiver_id, message_text)                .....                $context = new \ZMQContext();                $socket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'my pusher');                $socket->connect("tcp://localhost:5555");                $pushData = array('receiver_id' => $receiver_id,'sender_id'  => $user->getId(),'message'  => $message->getMessageText(),                    );                $socket->send(json_encode($pushData));

So at the end, my websocket server is able to know which is the id of receiver using the JSON. But how will he know which is the connection of that user? In other words, I need to store websocket connections in an array that is indexed by the user id.

<?phpnamespace RealTime;use Ratchet\MessageComponentInterface;use Ratchet\ConnectionInterface;use Ratchet\Wamp\WampServerInterface;class Pusher implements WampServerInterface, MessageComponentInterface{    private $clients;    public function onOpen(ConnectionInterface $conn) {        $this->clients[$conn->resourceId] = $conn;        // I need here to get the user_id received from browser while opening connection    }    public function onMessageEntry($entry) {        $entryData = json_decode($entry, true);        //This is not what I need (It sends to all users in array)        foreach ($this->clients as $key => $client) {        $client->send($entryData['message']);         }    }    public function onMessage(ConnectionInterface $from, $msg) {        echo $msg;     }}

And the websocket server:

<?php        require dirname(__DIR__) . '/vendor/autoload.php';        use RealTime\Pusher;        $loop   = React\EventLoop\Factory::create();        $pusher = new Pusher;        $context = new React\ZMQ\Context($loop);        $pull = $context->getSocket(ZMQ::SOCKET_PULL);        $pull->bind('tcp://127.0.0.1:5555');         $pull->on('message', array($pusher, 'onMessageEntry'));        $webSock = new React\Socket\Server($loop);        $webSock->listen(8080, '0.0.0.0');         $webServer = new Ratchet\Server\IoServer(            new Ratchet\Http\HttpServer(                new Ratchet\WebSocket\WsServer(                    new Ratchet\Wamp\WampServer(                        $pusher                    )                )            ),            $webSock        );        $loop->run();        ?>

Questions:

  1. How to send the logged in user_id from client side while opening connection.I need to have the value in websocket server so that I can index my array of clients with it ($client[user_id]=$conn instead of $client[recourceId]=$conn). I tried the javascript function send but I don't know where to receive the sent data (even onMessage is not printing anything).

  2. Why the onMessage method is not executing even MessageComponentInterface implemented (Is it because I have onMessageEntry method +$pull->on('message', array($pusher, 'onMessageEntry')); line of code?

Thank you.


Viewing all articles
Browse latest Browse all 208

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>