Method
OrderedChannel::write
Append $message to the channel, where every subscriber receives it in the channel's one order.
Description#
public OrderedChannel::write(
mixed $message
): voidThe message is encoded as Swerve::publish does. While the channel has no subscriber
anywhere nothing is written and the message is dropped.
Parameters#
- $message mixed
- Anything
json_encode()takes except null; at most 128 KiB encoded
Errors / Exceptions#
| Exception | When |
|---|---|
InvalidArgumentException | As Swerve::publish() does, and for a name starting with "\0" |
LogicException | Directly in swerve.php, which the worker serves after: a coroutine started there waits instead |
JsonException | For a value JSON can't express |
Examples#
use Swerve\OrderedChannel;
phasync::run(function () {
$events = new OrderedChannel('events');
$events->write('lost'); // a broadcast: nobody subscribes, nothing is kept
$subscription = $events->subscribe(); // receives what is written from here on
$events->write('seen');
foreach ($subscription as $event) {
echo "$event\n";
break;
}
});Output
seen
$ledger = new OrderedChannel('ledger');
$ledger->write(['debit', 12]);Notes#
Note
Unlike Swerve::publish()/publishRaw(), the first write() to a channel (or the first after the master forgets its bitmap) suspends the caller until the master answers: whether to append at all depends on its reply, so, unlike a plain topic's message, the write itself cannot be queued ahead of knowing it.
See also#
OrderedChannel::subscribeReceive what is written from now on, asSwerve::subscribedoes.Swerve::publishSend a message to every subscriber of$topic, in every worker process of this swerve, this one included.