Core\Socket\Message::topic
$message->topic(): ?stringReturns the name of the topic a message was published to.
A connection receives messages from two places: from the client, and from the topics it subscribed to with Core\Topic::subscribe. topic() tells them apart. For a message from a topic, it returns the name of the topic. For a message from the client, it returns null.
A connection that subscribes to more than one topic uses the name to decide what to do with each message. The name is not tainted, because your program chose it when it subscribed.
The examples below tell a message from the client from a message from a topic, handle two topics in one loop, and forward the messages of two chat rooms to the client.
Return value
Returns ?string. The topic name for a message from a topic. It is null for a message from the client. The name is not tainted, because your program chose it when it subscribed.
Examples
Where a message came from
<?nvs// Shows how to tell a message from the client from a message from a topic.
// The `.nvsp` file beside this example plays a client that sends one text message.Core\Socket $conn = Core\Socket::current();Core\Topic::subscribe('news');Core\Topic::publish('news', 'the shop opens at nine');
// `topic()` returns the name of the topic, or `null` for a message from the client.// This prints `from news`, then `from the client`.?Core\Socket\Message $message = $conn->receive();while ($message != null) { echo 'from ', $message->topic() ?? 'the client', "\n"; $message = $conn->receive();}from newsfrom the clientTwo topics in one loop
<?nvs// Shows one loop that handles the messages of two topics in different ways.
// The `.nvsp` file beside this example plays a client that sends nothing and closes.Core\Socket $conn = Core\Socket::current();Core\Topic::subscribe('prices');Core\Topic::subscribe('alerts');
// In a real application, other scripts publish these values.Core\Topic::publish('prices', 42);Core\Topic::publish('alerts', 'the market closes in ten minutes');
// The name of the topic decides how the value is read.// This prints `price: 42`, then `alert: the market closes in ten minutes`.?Core\Socket\Message $message = $conn->receive();while ($message != null) { if ($message->topic() == 'prices') { echo 'price: ', $message->value() as int, "\n"; } elseif ($message->topic() == 'alerts') { echo 'alert: ', $message->value() as string, "\n"; } $message = $conn->receive();}price: 42alert: the market closes in ten minutesTwo chat rooms
<?nvs// Shows a chat connection that is in two rooms and tells the client which room each line is from.
// The `.nvsp` file beside this example plays a client that sends nothing and closes.Core\Socket $conn = Core\Socket::current();Core\Topic::subscribe('room:lobby');Core\Topic::subscribe('room:games');
// In a real application, the other people in each room publish these lines.Core\Topic::publish('room:lobby', 'good morning');Core\Topic::publish('room:games', 'who wants to play?');
// `Core\Str::after` removes the `room:` part of the topic name.// This prints `sent: [lobby] good morning`, then `sent: [games] who wants to play?`.?Core\Socket\Message $message = $conn->receive();while ($message != null) { string $topic = $message->topic() ?? ''; string $room = Core\Str::after($topic, 'room:') ?? $topic; $conn->send('[' . $room . '] ' . $message->value() as string); $message = $conn->receive();}sent: [lobby] good morningsent: [games] who wants to play?