Skip to content

Core\Sse::receive

$sse->receive(): ?Core\Sse\Message

Waits for the next value published to a topic that this event stream subscribed to.

A script that Core\Sse::upgrade starts keeps a stream open to one client. It calls Core\Topic::subscribe for the topics it wants. Then it calls receive() in a loop. Each call waits until another script publishes a value on one of those topics, and returns it as a Core\Sse\Message. The message has the value and the name of its topic.

receive() returns null when the stream is over. This happens when the client has closed the connection, or when the stream fell too far behind and its queue of values was full. A while loop over receive() then ends by itself.

receive() works only in a script that Core\Sse::upgrade started. A request that answers with Core\Sse::startStream ends with its own events, so receive() throws a LogicError there.

The examples below forward every update to the client, show that only subscribed topics arrive, and send the steps of a deployment to a status page.

Return value

Returns ?Core\Sse\Message. The next message, or null when the stream has ended. The null ends a while (var $msg = $sse->receive()) loop. A message has the published value and the name of its topic. A stream that falls too far behind also gets null. Its queue is full, so the stream is closed and publishers do not wait for it.

Errors

  • LogicErrorThis stream was opened by startStream, not by upgrade. It ends with the request, so no published value can reach it, and there is nothing to wait for.
  • ParseErrorA published object has a class that this program does not declare, or its properties do not match that class. The error names the topic and the class. The message is removed, so the next call returns the next message.

Examples

Forwarding every update

<?nvs
// Shows a stream script that sends every published update to its client.
// The `.nvse` file beside this example publishes two updates on `orders`.
Core\Sse $sse = Core\Sse::current();
Core\Topic::subscribe('orders');
// `receive()` waits for the next published value and returns it as a message.
// When the client has gone, it returns `null` and the loop ends.
?Core\Sse\Message $message = $sse->receive();
while ($message != null) {
// This prints `event: order` and one `data:` line for each update.
$sse->send($message->value() as string, event: 'order');
$message = $sse->receive();
}
// This prints `stream closed`.
echo "stream closed\n";
Output
event: order
data: order 1042 was paid
event: order
data: order 1042 has shipped
stream closed

Only subscribed topics arrive

<?nvs
// Shows that a stream receives values only from the topics it subscribed to.
// The `.nvse` file beside this example publishes one value on `news:weather`
// and one on `news:sport`. This script subscribes to `news:sport` only.
Core\Sse $sse = Core\Sse::current();
Core\Topic::subscribe('news:sport');
// `topic()` returns the topic a message came from.
// This prints `news:sport: the match starts at 8` and nothing for the weather.
?Core\Sse\Message $message = $sse->receive();
while ($message != null) {
echo $message->topic(), ': ', $message->value() as string, "\n";
$message = $sse->receive();
}
Output
news:sport: the match starts at 8

Deploy status page

<?nvs
// Shows a stream that sends the progress of one deployment to a status page.
// The `.nvse` file beside this example publishes three steps of deployment 42.
// Another script, such as the deploy job, publishes these in a real application.
Core\Sse $sse = Core\Sse::current();
Core\Topic::subscribe('deploy:42');
?Core\Sse\Message $message = $sse->receive();
while ($message != null) {
tainted string $step = $message->value() as string;
// This prints one `status` event for each step.
$sse->send($step, event: 'status');
// The script stops by itself after the last step. It does not wait
// until the client closes the page.
if ($step == 'done') {
break;
}
$message = $sse->receive();
}
Output
event: status
data: building
event: status
data: testing
event: status
data: done