Skip to content

Core\Topic::publish

Sends a value to every connection that subscribed to a topic, and returns how many connections it was sent to.

Any script can publish. An ordinary web request, a command-line job or a connection can all tell the subscribers that something changed. Each subscriber gets its own copy of the value, so a change one of them makes is not seen by the others. An object arrives as the class with the same name in the subscriber's program. publish never waits for a subscriber. A subscriber with too many unread messages is skipped, and its connection is closed.

Good to know: a value that contains a secret cannot be published, and publish throws a LogicError. This happens even when nobody subscribed to the topic.

The examples below publish to a topic nobody joined, try to publish a secret, and tell the open staff dashboards about a new order.

Parameters

  • $topicstringThe name of the topic. As with subscribe, it may not be tainted, so a name built from user input does not compile.
  • $valuemixedThe value to send. Each subscriber gets its own copy, so a change one subscriber makes is not seen by any other. An object arrives as the class with the same name in the subscriber's program. A tainted value is still tainted when it arrives. A value that contains a secret cannot be published.

Return value

Returns uint. The number of subscribers the value was sent to. It is 0 for a topic nobody joined. Any script can publish, so an ordinary web request can tell the connections that something changed. A connection that publishes to a topic it joined also receives the value. A subscriber with too many unread messages is skipped and not counted. It is being closed, and publish never waits for it.

Errors

  • LogicError$topic is empty or longer than 256 bytes, or $value cannot be sent to another connection. Examples are an object with a secret property, a function, a generator and an open file. The error is thrown even when nobody joined the topic.

Examples

Publishing to an empty topic

<?nvs
// Shows that any script can publish, and that `publish` returns how many connections received the value.
// Nobody subscribed to `room:lobby`, so `publish` returns `0`.
uint $reached = Core\Topic::publish('room:lobby', 'hello');
echo $reached, "\n";
// This prints `nobody is listening`.
if ($reached == 0) {
echo "nobody is listening\n";
}
Output
0
nobody is listening

A value with a secret

<?nvs
// Shows that a value with a `secret` property cannot be published.
// A `secret` property is a value the program must not show or send.
// It is never copied to another connection.
class Login
{
public string $user = 'ana';
public secret string $password = 'correct horse';
}
try {
Core\Topic::publish('logins', new Login());
} catch (LogicError $error) {
// This prints the error message, which names the `secret` property.
echo $error->message, "\n";
}
// Without the password, the value can be published.
// Nobody subscribed to `logins`, so this prints `0`.
echo Core\Topic::publish('logins', ['user' => 'ana']), "\n";
Output
`Core\Topic::publish` cannot publish this value: Login::$password is `secret` and does not cross a copy boundary unless it is revealed
0

Announcing a new order

<?nvs
// Shows a checkout that tells every open staff dashboard about a new order.
// The `.nvsp` file beside this example plays one staff dashboard that sends nothing and then closes.
// This script is the connection of that dashboard, and it joins the `orders` topic.
Core\Socket $conn = Core\Socket::current();
Core\Topic::subscribe('orders');
class Checkout
{
// In a real application, the checkout request calls this method.
public static function announce(string $id, string $total): uint
{
return Core\Topic::publish('orders', ['id' => $id, 'total' => $total]);
}
}
// One dashboard is open, so this prints `sent to 1 dashboard(s)`.
echo 'sent to ', Checkout::announce('1042', '25.50'), " dashboard(s)\n";
// The dashboard's connection receives the order and sends it to the browser as JSON.
?Core\Socket\Message $message = $conn->receive();
while ($message != null) {
$conn->send(Core\Json::encode($message->value()));
$message = $conn->receive();
}
Output
sent to 1 dashboard(s)
sent: {"id":"1042","total":"25.50"}