Integrating ZMQ subscriber messages into example websocket chat server

66 views
Skip to first unread message

Craig Giffen

unread,
Aug 21, 2015, 7:35:39 PM8/21/15
to Ratchet
I'm trying to integrate the "subscriber" portion of a ZMQ pub/sub into the chat server example.

Situation:
I have the example websocket chat server running fine, but I sometimes need to be able to have a separate application (the publisher) on the backend pass a message via ZMQ to the websocket server (the subscriber) which it turn the message can be sent out to all of the connected websocket chat clients.  One use case would be say, if the server needs to be taken offline soon, I need to be able to run a quick script (the zmq publisher) which will notify every websocket server (the zmq subscribers) of the scheduled downtime so they can in turn tell all of their connected websocket clients they will be logged off the chat soon. 

I basically need to integrate #1 into #2 below, so when $mystring receives a value, the chat server can send it out to all of its connected clients via the websocket send() method. I'm just not sure how to monitor $subscriber->recv() for activity and if it occurs, to have the chat server handle it accordingly.

1) Working subscriber code:

$context = new ZMQContext();
$subscriber
= $context->getSocket(ZMQ::SOCKET_SUB);
$subscriber
->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, '');

$subscriber
->connect('tcp://127.0.0.1:5555');
while(true) {

    $mystring
= $subscriber->recv();
    echo
"We received ".$mystring."\n"; ;
 
}




2) Chat server:

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;



$server
= IoServer::factory(
       
new HttpServer(
               
new WsServer(
                       
new Chat()
               
)
       
),
       
'8080','127.0.0.1'
);


$server
->run();



thanks,
Craig

Reply all
Reply to author
Forward
0 new messages