Can i send a data from RabbitMQ to ratchet connection in php

24 Views Asked by At

What we did earlier was making connection and subscribe using Ratchet ZMQ SUB/PUB method. Also, data will send through ZMQ context and data will send to the subscribed user.

Now, i'm trying to replace RabbitMQ over ZMQ. But, as per my knowledge we won't differentiate the websocket connection without subscribtion. So, Subscription method available via ZMQ only.

So, i'm confused about the RabbitMQ connection. How can i differntiate the websocket connection to send the data to specific user with integrating rabbitMQ

In Js file

var conn = new ab.Session(
                            'ws://localhost:8000' 
                          , function() {            
                                conn.subscribe(id, function(topic, data) {
                                    console.log(topic);
                                    console.log(data);
                                   
                                });
                            }
                        );

Server file

require dirname(__DIR__) . '/vendor/autoload.php';

$loop   = React\\EventLoop\\Factory::create();
$pusher = new MyApp\\Pusher;

// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\\ZMQ\\Context($loop);
$pull = $context-\>getSocket(ZMQ::SOCKET_PULL);
$pull-\>bind('tcp://localhost:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull-\>on('message', array($pusher, 'onBlogEntry'));

// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\\Socket\\Server('0.0.0.0:8000', $loop); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\\Server\\IoServer(
    new Ratchet\\Http\\HttpServer(
        new Ratchet\\WebSocket\\WsServer(
            new Ratchet\\Wamp\\WampServer(
                $pusher
            )
        )
    ),
    $webSock
);`

$loop-\>run();

Send_data

 $dsn = "tcp://localhost:5555";
$socket = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_PUSH, 'my pusher');
$endpoints = $socket-\>getEndpoints();

         /\* Check if the socket is connected \*/
      if (!in_array($dsn, $endpoints\['connect'\])) {
          $socket-\>connect($dsn);
      } else {
         //    echo "\<p\>Already connected to $dsn\</p\>";
      }
      
      /\* Send and receive \*/
      $socket-\>send(json_encode($publish_data));

Pusher file

public function onBlogEntry($entry) {
$publish_data    = json_decode($entry, true);
$subscriber_list = $publish_data\['subscriber'\];

    if (is_array($subscriber_list) || is_object($subscriber_list))
    {   
        foreach ($subscriber_list as $subscriber_list_val) {
            if (!array_key_exists($subscriber_list_val, $this-\>subscribedTopics)) {
                return;
            }else{
                $subsciber = $subscriber_list_val;  
                $topic = $this-\>subscribedTopics\[$subsciber\];
                $topic-\>broadcast($publish_data);  // re-send the data to all the clients subscribed to that category
            }
        }
    } 
}
0

There are 0 best solutions below