0

I have this simple code for a ZeroMQ pub/sub setup in PHP , and the strange part is it doenst work for single (1x) web Requests, when I comment out the while(true) { //code } it doesn't work the web client doesn't receive the single request, but when the publisher->send() is invoked inside the loop the web client receives all the message normally.

ZeroMQ PHP Publisher

    <?php
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache'); // recommended to prevent caching of event dat
    header("Connection: keep-alive");

    $port=8899; 
    //  Prepare our context and publisher
    $context = new ZMQContext();
    $publisher = $context->getSocket(ZMQ::SOCKET_PUB);
    $publisher->bind("tcp://*:".$port);

    echo "Running..Serving test data on TCP port $port every 1 s \n <hr>";
    while (true) {  //doesn't work when this line is commented out

        $count++;
        $json=json_encode(array($count, PHP_RELEASE_VERSION, PHP_OS , $_SERVER['PHP_SELF'], Date("h:i:s m-d-Y")  ) );
        $publisher->send( $json);
        sleep(1);
        echo "+";   
        ob_flush();
       flush();  //send out to browser
     }
    ?>

ZeroMQ PHP Subscriber

<?php
header('Cache-Control: no-cache'); // recommended to prevent caching of event dat
header("Connection: keep-alive");

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

$port=8899;
$subscriber->connect('tcp://localhost:'.$port);

while(true) {

    echo "<br>Waiting for message " ;
    $string = $subscriber->recv();
    echo "<br>Received ".$string ;
    ob_flush();
    flush();
 }

?>

I suspect it has to do with when the PHP script ends the ZeroMQ message is never sent or closed for single requests..

1 Answer 1

1

The problem is related to the "slow joiner" symptom of ZeroMQ in a pub/sub situation, as it's discussed here in their guide:

There is one more important thing to know about PUB-SUB sockets: you do not know precisely when a subscriber starts to get messages. Even if you start a subscriber, wait a while, and then start the publisher, the subscriber will always miss the first messages that the publisher sends. This is because as the subscriber connects to the publisher (something that takes a small but non-zero time), the publisher may already be sending messages out.

The issue in the code above is all on the ZeroMQ PHP Publisher script.. basically because of the time it takes ZeroMQ to setup the Socket and do it's prep work (small but non-zero time), there is a small delay when the publisher first sets up the MQ.

The tricky part is that it returns immediately after calling :

 //  Prepare our context and publisher
$context = new ZMQContext();
$publisher = $context->getSocket(ZMQ::SOCKET_PUB);
$publisher->bind("tcp://*:".$port);  //returns immediately BUT NOT REALLY ready

then of course the rest of above PHP runs through in less than 10ms so when you issue the actual send command is issued, the MQ isn't setup yet, and that initial message is lost.

   $publisher->send( $json);  //send the message but MQ not REALLY setup just yet.

This is of course what causes this confusion since in programming you expect that when a function (like $publisher->bind ) returns successfully it has completed it's task, but in reality it has not..

The simple solution is to use a small delay (you need to play with the delay value) right after the bind to give ZeroMQ a chance to finish its work. Now this approach is not the most elegant as the delay amount could vary by machine etc, and the guide at ZeroMq tells you about this..

The folks at ZeroMQ recommend to use a more sophisticated approach keeping track of subscribers and having a back-channel that joining subscribers can communicate with the publisher when they have joined..

For my purposes that approach is overkill so the command below works fine, just give the usleep value enough time..

  $port=8899; 
//  Prepare our context and publisher
$context = new ZMQContext();
$publisher = $context->getSocket(ZMQ::SOCKET_PUB);
$publisher->bind("tcp://*:".$port);
usleep(250000);   //wait 0.25 secs enougth for ZeroMQ to build the socket.

That worked for me...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.