phasync
OrderedChannel / write

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
): void

The 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#

ExceptionWhen
InvalidArgumentExceptionAs Swerve::publish() does, and for a name starting with "\0"
LogicExceptionDirectly in swerve.php, which the worker serves after: a coroutine started there waits instead
JsonExceptionFor a value JSON can't express

Examples#

Example 1Nothing is kept without a subscriber
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
Example 2
$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#