Skip to content

No messages received by SUB when binding with connecting PUB #166

@rikvdh

Description

@rikvdh

Hi,

I'm having a situation where I bind an subscriber and connect with a publisher. I already spent several hours trying to get it working.

I have an application in C++ which receives and sends. When a put my implementation in pure C++ (without php-zmq) it works. But having the subscriber in PHP having to bind the socket it doesn't work.

My publisher:

<?php

$context = new ZMQContext();
$sender = new ZMQSocket($context, ZMQ::SOCKET_PUB);
$sender->connect("tcp://127.0.0.1:7001");
for ($i = 0; $i < 6; $i++) {
        echo '['.getmypid()."] send " . $i . PHP_EOL;
        $sender->send("message " . $i);
}

My subscriber:

<?php

        echo '['.getmypid()."] starting child..." . PHP_EOL;
        $childctx3 = new ZMQContext();
        $logging = new ZMQSocket($childctx3, ZMQ::SOCKET_SUB);
        $logging->bind("tcp://*:7001");

        sleep(1);
        $logging->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, "");
        sleep(1);
        for ($i = 0; $i < 6; $i++) {
                echo '['.getmypid()."] going into recv.." . PHP_EOL;
                $rep = $logging->recv();
                echo '['.getmypid()."] return: " . print_r($rep, true) . PHP_EOL;
        }
        echo '['.getmypid()."] done running child..." . PHP_EOL;

This doesn't work. But when I swap the subscriber with a C++ application with a binding subscribe ZMQ socket it works.

Also reversing bind and connect in the above examples works, then the messages pass through.

The funny thing is that I see the connection established in netstat and also the subscriber listening on the socket.

tcp        0      0 0.0.0.0:7001            0.0.0.0:*               LISTEN      18049/php
tcp        0      0 127.0.0.1:50940         127.0.0.1:7001          ESTABLISHED 18097/php
tcp        0      0 127.0.0.1:7001          127.0.0.1:50940         ESTABLISHED 18049/php

I'm using:

  • latest php-zmq from git
  • ZMQ 4.1.3
  • PHP 5.6.7
  • OS: Debian 8.2

Can someone please explain what goes wrong?

Thanks,

Rik

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions