phasync
Channels and messaging / SubscriberInterface

Interface

SubscriberInterface

A subscription to a publisher: a reading end that receives every message published after it was made.

Introduction#

It is read like any ReadChannelInterface, with read() or foreach. A subscription that falls behind does not hold the publisher or the other subscriptions back, and it ends once the publisher is closed and its messages are read.

Class synopsis#

interface SubscriberInterface extends ReadChannelInterface, \IteratorAggregate, \Traversable, SelectableInterface {

}

Examples#

Example 1
phasync::run(function () {
    phasync::publisher($subscribers, $publisher);
    $subscription = $subscribers->subscribe();

    phasync::go(function () use ($publisher) {
        $publisher->write('a');
        $publisher->write('b');
        $publisher->close();
    });

    foreach ($subscription as $message) {
        echo $message, "\n";      // a, then b
    }
});

See also#