Method
phasync::publisher
Creates a publisher: what is written to $publisher is delivered to every subscription of $subscribers.
Description#
public static phasync::publisher(
?SubscribersInterface &$subscribers,
?WriteChannelInterface &$publisher
): voidEach subscription receives the messages written after it was made, in order, and a slow subscription does not hold back the others. A subscription made later does not see earlier messages. Writing with no subscriber succeeds. Closing the publisher ends the subscriptions once they have read what was written.
A publisher forwards messages in a phasync::service coroutine, which ends when the publisher is closed or its write end is dropped, and is cancelled when the outermost run() has nothing else left to do.
Parameters#
- $subscribers ?SubscribersInterface
- Receives the object to subscribe with
- $publisher ?WriteChannelInterface
- Receives the writing end
Examples#
phasync::run(function () {
phasync::publisher($subscribers, $publisher);
foreach ([1, 2] as $i) {
$subscription = $subscribers->subscribe();
phasync::go(function () use ($subscription, $i) {
foreach ($subscription as $event) {
echo "subscriber $i got $event\n";
}
});
}
$publisher->write('deployed');
$publisher->close();
});See also#
phasync::channelWhen each value goes to one readerSubscribersInterfaceThe subscription point of a publisher: each call tosubscribe()makes a reader that receives every later message.