1

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?

Community
  • 1
  • 1
Michael Osl
  • 2,720
  • 1
  • 21
  • 36
  • Did you try to explicitly XSUB-scribe the [ PROXY ] to any topic: `$frontend->send( ( ascii 1 ) + "" ); /* XSUBSCRIBE to ANY topic incoming */ ` ? ( **API-Ref.: >>>** http://api.zeromq.org/3-2%3azmq-socket#toc12 ) – user3666197 Nov 23 '16 at 16:39
  • 1
    That's it! I added `$frontend->send(chr(1));` to the proxy and it works. Please feel free to convert this to an answer so I can mark it as accepted. – Michael Osl Nov 23 '16 at 16:54
  • Glad it helped. Salutes to Vienna ( nice homepage :o) ) – user3666197 Nov 23 '16 at 17:04
  • Thanks! Best wishes to Prague :) – Michael Osl Nov 23 '16 at 17:45

1 Answers1

1

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

user3666197
  • 1
  • 6
  • 50
  • 92