Search code examples
phpzeromqpublish-subscribe

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

subscriber.php

<?php
$context = new ZMQContext();
$sub = new ZMQSocket($context, ZMQ::SOCKET_SUB);
$sub->connect("tcp://127.0.0.1:5000");
$sub->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, 'Hello');
$msg = $sub->recv();
echo "got $msg";

publisher.php

<?php

$context = new ZMQContext();
$pub = new ZMQSocket($context, ZMQ::SOCKET_PUB);
$pub->connect("tcp://127.0.0.1:6000");

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

proxy.php

<?php
$context = new ZMQContext();
$frontend = new ZMQSocket($context, ZMQ::SOCKET_XSUB);
$frontend->bind("tcp://127.0.0.1:6000");
$backend = new ZMQSocket($context, ZMQ::SOCKET_XPUB);
$backend->bind("tcp://127.0.0.1:5000");
$device = new ZMQDevice($frontend, $backend);
$device->run();

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?


Solution

  • [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 )