SAREhub/PHP_Commons

View on GitHub
examples/zmq/publish_subscribe/1/subscriber.php

Summary

Maintainability
B
4 hrs
Test Coverage
<?php

use SAREhub\Commons\Misc\Dsn;
use SAREhub\Commons\Zmq\PublishSubscribe\Subscriber;

require './ExampleCommons.php';

try {
    $subscriber = Subscriber::inContext(new ZMQContext())->connect(Dsn::tcp()->endpoint("127.0.0.1:10000"));
    $subscriber->subscribe("topic");
    logMessage("Connected");
    logMessage("Waiting for message");

    for ($i = 1; $i < 20; ++$i) {
        logMessage("try receive $i");
        if ($message = $subscriber->receive()) {
            logMessage("Message: " . print_r($message, true));
            break;
        }
        sleep(1);
    }

    $subscriber->disconnectAll();
    logMessage("Disconnected");
} catch (Exception $e) {
    echo $e;
}