Skip to content

Core\Topic::unsubscribe

Core\Topic::unsubscribe(string $topic): voidCore\Topic::unsubscribe(
    string $topic,
): void

Removes the current connection from a topic, so values published to that topic no longer reach it.

Use it when a connection should stop listening to one topic but stay open. For example, a user leaves one chat room and stays in the others. You do not need it when a connection ends: a connection that closes leaves all its topics without any extra code. Leaving a topic the connection never joined is not an error.

Good to know: only values published after the call stop arriving. A value that was already waiting still arrives at the next receive().

The examples below leave one of two topics, leave a topic that was never joined, and let a chat user leave a room.

Parameters

  • $topicstringThe name of the topic. As with subscribe, it may not be tainted.

Return value

Returns void. Nothing. Leaving a topic this connection never joined is not an error.

Errors

  • LogicError$topic is empty or longer than 256 bytes, or this script is not a WebSocket connection or an event stream. subscribe throws the same errors.

Examples

Leaving one of two topics

<?nvs
// Shows a connection that stops listening to one topic and keeps the other.
// The `.nvsp` file beside this example plays a client that sends nothing and then closes.
Core\Socket $conn = Core\Socket::current();
Core\Topic::subscribe('news:weather');
Core\Topic::subscribe('news:sport');
Core\Topic::publish('news:weather', 'rain in the afternoon');
Core\Topic::publish('news:sport', 'the match starts at 8');
// The connection leaves `news:sport`. The sport news above is already
// waiting, so it still arrives. The sport news below does not.
Core\Topic::unsubscribe('news:sport');
Core\Topic::publish('news:weather', 'sun in the evening');
Core\Topic::publish('news:sport', 'the match ended 2 to 1');
// This prints three lines: two about the weather and one about sport.
?Core\Socket\Message $message = $conn->receive();
while ($message != null) {
echo $message->topic(), ': ', $message->value() as string, "\n";
$message = $conn->receive();
}
Output
news:weather: rain in the afternoon
news:sport: the match starts at 8
news:weather: sun in the evening

A topic never joined

<?nvs
// Shows that leaving a topic the connection never joined is not an error.
// The `.nvsp` file beside this example plays a client that sends nothing and then closes.
// This connection never joined `room:lobby`. The call does nothing and throws no error.
Core\Topic::unsubscribe('room:lobby');
// Nobody is subscribed to `room:lobby`, so this prints `0`.
echo Core\Topic::publish('room:lobby', 'hello'), "\n";
Output
0

Leaving a chat room

<?nvs
// Shows a chat connection where the user leaves one room and stays in another.
// The `.nvsp` file beside this example plays a client. It sends a line, then
// the command `/leave random`, then another line, and then closes.
Core\Socket $conn = Core\Socket::current();
Core\Topic::subscribe('room:lobby');
Core\Topic::subscribe('room:random');
// This prints three `sent:` lines. The second line from the client
// reaches only `room:lobby`, because the user left `room:random`.
?Core\Socket\Message $message = $conn->receive();
while ($message != null) {
?string $topic = $message->topic();
tainted string $text = $message->text() ?? '';
if ($topic != null) {
// A message from a room goes to the client.
$conn->send($topic . ': ' . ($message->value() as string));
} elseif ($text == '/leave random') {
// The text from the client is `tainted`, so it cannot be a topic name.
// The program checks the text and then uses its own name for the room.
Core\Topic::unsubscribe('room:random');
} else {
// Every other line from the client is posted to both rooms.
Core\Topic::publish('room:lobby', $text);
Core\Topic::publish('room:random', $text);
}
$message = $conn->receive();
}
Output
sent: room:lobby: hello everyone
sent: room:random: hello everyone
sent: room:lobby: see you later