Skip to content

Core\Socket\Message::value

$message->value(): mixed

Returns the value that was published to a topic.

A connection that subscribes to a topic with Core\Topic::subscribe receives every value published to it. receive() returns each one as a message, and value() returns the value that was published: a number, a string or an array. For a message from the client, value() returns null.

The value is a copy. If you change it, the value the publisher has does not change. It comes from your own application and not from the client, so value() does not make it tainted.

The examples below read a published number, tell a message from the client from a published value, and send a live score to the client each time it changes.

Return value

Returns mixed. A copy of the published value. Changing the copy does not change the value the publisher has. It is null for a message from the client.

Examples

Reading a published value

<?nvs
// Shows how to read a value that was published to a topic.
// The `.nvsp` file beside this example plays a client that sends nothing and closes.
Core\Socket $conn = Core\Socket::current();
Core\Topic::subscribe('prices');
// In a real application, another script publishes the price.
// Here the same script publishes it, so the example runs alone.
Core\Topic::publish('prices', 42);
// `value()` returns the published value. This prints `prices: 42`.
?Core\Socket\Message $message = $conn->receive();
if ($message != null) {
int $price = $message->value() as int;
echo $message->topic() ?? '', ': ', $price, "\n";
}
Output
prices: 42

A client message has no value

<?nvs
// Shows that `value()` returns `null` for a message from the client.
// The `.nvsp` file beside this example plays a client that sends one text message.
Core\Socket $conn = Core\Socket::current();
Core\Topic::subscribe('alerts');
Core\Topic::publish('alerts', 'disk almost full');
// The published value arrives first, then the client's message.
// This prints `from the topic: disk almost full`, then `from the client: hello`.
?Core\Socket\Message $message = $conn->receive();
while ($message != null) {
mixed $value = $message->value();
if ($value == null) {
echo 'from the client: ', $message->text() ?? '', "\n";
} else {
echo 'from the topic: ', $value as string, "\n";
}
$message = $conn->receive();
}
Output
from the topic: disk almost full
from the client: hello

A live score

<?nvs
// Shows a connection that sends the live score of a match to the client each time it changes.
// The `.nvsp` file beside this example plays a client that sends nothing and closes.
Core\Socket $conn = Core\Socket::current();
Core\Topic::subscribe('match:final');
// In a real application, another script publishes the score when a team scores.
// Here the same script publishes two scores, so the example runs alone.
Core\Topic::publish('match:final', ['home' => 1, 'away' => 0]);
Core\Topic::publish('match:final', ['home' => 1, 'away' => 1]);
// `value()` returns each published score. This sends it to the client as JSON.
// This prints `sent: {"home":1,"away":0}`, then `sent: {"home":1,"away":1}`.
?Core\Socket\Message $message = $conn->receive();
while ($message != null) {
$conn->send(Core\Json::encode($message->value()));
$message = $conn->receive();
}
Output
sent: {"home":1,"away":0}
sent: {"home":1,"away":1}