So, I start the Ratchet web server server (php) with several routes that connect several Ratchet applications (MessageComponentInterfaces):
//loop $loop = \React\EventLoop\Factory::create(); //websocket app $app = new Ratchet\App('ws://www.websocketserver.com', 8080, '0.0.0.0', $loop); /* * load routes */ $routeOne = '/example/route'; $routeOneApp = new RouteOneApp(); $app->route($routeOne, $routeOneApp, array('*')); $routeTwo = '/another/route'; $routeTwoApp = new AnotherApp(); $app->route($routeTwo, $routeTwoApp, array('*'));
From here, I bind the ZMQ socket to be able to receive messages sent from php scripts running on a regular Apache server.
// 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://127.0.0.1:5050'); // Binding to 127.0.0.1 means the only client that can connect is itself $pull->on('message', array($routeOneApp, 'onServerMessage'));
Finally, the server is running:
//run $loop->run();
This works fine as long as I bind only one ratchet app to the ZMQ socket. However, I would like to be able to separately send messages to both Ratchet applications. For this, I was thinking of binding two ZMQ sockets to different routes, for example:
$pullOne->bind('tcp://127.0.0.1:5050' . $routeOne); // Binding to 127.0.0.1 means the only client that can connect is itself $pullOne->on('message', array($routeOneApp, 'onServerMessage'));
and
$pullTwo->bind('tcp://127.0.0.1:5050' . $routeTwo); // Binding to 127.0.0.1 means the only client that can connect is itself $pullTwo->on('message', array($routeTwoApp, 'onServerMessage'));
However, this leads to an error message from ZMQ when binding a second socket, saying that this address is already in use.
So the question is, is there any other way to use routes through a ZMQ socket? Or should I use other means to distinguish between messages for individual Ratchet applications, and if so, what would be a good solution? I was thinking of binding to two different ports, but realized that this would be a pretty ugly solution?