Michael Osl Michael Osl - 9 months ago 60
PHP Question

ZeroMQ XPub-XSub with PHP: Messages never arrive at proxy

I'm trying to implement Publish Subscribe using a Proxy with ZeroMQ and PHP as it is described in the guide in Figure 13. The setup is the same as described here: how to implement Pub-Sub Network with a Proxy by using XPUB and XSUB in ZeroMQ(jzmq) 3.xx


$context = new ZMQContext();
$sub = new ZMQSocket($context, ZMQ::SOCKET_SUB);
$sub->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, 'Hello');
$msg = $sub->recv();
echo "got $msg";



$context = new ZMQContext();
$pub = new ZMQSocket($context, ZMQ::SOCKET_PUB);

while (1) {
echo "publishing";
$pub->send("Hello World");


$context = new ZMQContext();
$frontend = new ZMQSocket($context, ZMQ::SOCKET_XSUB);
$backend = new ZMQSocket($context, ZMQ::SOCKET_XPUB);
$device = new ZMQDevice($frontend, $backend);

If I start all three PHP scripts (first proxy, then publisher, then subscriber) no messages arrive at that subscriber.

In order to see if any messages arrive at the proxy at all, I tried to receive the messages manually on the proxy:

while (true) {
if ($frontend->recv(ZMQ::MODE_DONTWAIT)) {
echo "received message from xpub";
if ($frontend->recv(ZMQ::MODE_DONTWAIT)) {
echo "received message from xsub";

There are serveral related questions on Stack Overflow:

What am I missing?

Answer Source

[PROXY] needs to have the topic-filter set too:

$frontend->send( chr(1) + "" ); /* XSUBSCRIBE to { ANY == "" } topic incoming */

( For ZeroMQ API-Ref.: >>> ZeroMQ API documentation )