Core\Topic::subscribe
Core\Topic::subscribe(string $topic): voidCore\Topic::subscribe( string $topic, ): void
Joins the current WebSocket connection or event stream to a topic, so it receives every value published to that topic.
A topic is a name, such as room:lobby or order:1042. After subscribe, a value that any script publishes to the topic arrives at the next receive() of this connection. The message's topic() returns the name, so you can tell it apart from a message the client sent. Only a script that Core\Socket::upgrade or Core\Sse::upgrade started can subscribe. When a connection ends, it leaves all its topics without any extra code. When one server runs two applications, each has its own topics, so the same name in both does not connect them.
Good to know: the topic name may not come from user input. Build it from values your program checked, such as the id of the signed-in user. A name has at most 256 bytes. One connection can join at most 1024 topics at the same time. Past either limit, subscribe throws a LogicError.
The examples below join a chat room, show the error outside a connection, and send the updates of one order to the customer's page.
Parameters
$topicstringThe name of the topic. It may not betainted(come from user input), so you build it from values your program checked. Otherwise the call does not compile. This stops one user from joining the topic of another user.
Return value
Returns void. Nothing. If a connection subscribes twice to the same topic, a published value still arrives only once.
Errors
LogicError$topicis empty or longer than 256 bytes, or this script is not a WebSocket connection or an event stream. Only a script thatCore\Socket::upgradeorCore\Sse::upgradestarted can subscribe. One connection can join at most 1024 topics at the same time, so joining one more also throws this error.
Examples
Joining a chat room
<?nvs// Shows a connection that joins a chat room and receives a message posted there.
// The `.nvsp` file beside this example plays a client that sends nothing and then closes.Core\Socket $conn = Core\Socket::current();Core\Topic::subscribe('room:lobby');
// Somebody posts to the room. In a real application, this is another connection.Core\Topic::publish('room:lobby', 'welcome to the lobby');
// This prints `room:lobby: welcome to the lobby`.?Core\Socket\Message $message = $conn->receive();while ($message != null) { echo $message->topic(), ': ', $message->value() as string, "\n"; $message = $conn->receive();}room:lobby: welcome to the lobbyOutside a connection
<?nvs// Shows what happens when a command-line script tries to subscribe.
// This script is not a WebSocket connection or an event stream, so it cannot// subscribe. `subscribe` throws a `LogicError`, and this prints its message.try { Core\Topic::subscribe('room:lobby');} catch (LogicError $error) { echo $error->message, "\n";}`Core\Topic::subscribe` needs a connection and this program is not one: only a script `Core\Socket::upgrade` or `Core\Sse::upgrade` opened has oneUpdates for one order
<?nvs// Shows an event stream that sends the status updates of one order to the customer's page.
// The `.nvse` file beside this example publishes updates for two orders.// In a real application, the shop's order system publishes them.Core\Sse $sse = Core\Sse::current();
// The topic name is built from a number the program already checked.// A name built from user input does not compile.int $orderId = 1042;Core\Topic::subscribe('order:' . ($orderId as string));
// This prints one `status` event for each update of order 1042.?Core\Sse\Message $message = $sse->receive();while ($message != null) { $sse->send($message->value() as string, event: 'status'); $message = $sse->receive();}event: statusdata: packed
event: statusdata: shipped