Skip to content

Core\Socket\Message::text

$message->text(): ?tainted string

Returns the text of a message that the client sent as text.

A WebSocket client can send two kinds of message: text and binary. text() returns the text of a text message, exactly as the client sent it. For a binary message it returns null, and bytes() returns the data instead. It also returns null for a message that came from a topic.

The text is tainted, because it comes from the client and a client can send anything. You can print it, compare it and send it back. Before you use it where only trusted text is allowed, check it with Core\Validate.

The examples below read what the client typed, tell a binary message from a text message, and read chat commands from the text.

Return value

Returns ?tainted string. The text, exactly as the client sent it. It is tainted, because it comes from the client. It is null for a binary message and for a message from a topic.

Examples

Reading what the client typed

<?nvs
// Shows how to read the text of each message the client sends.
// The `.nvsp` file beside this example plays the client.
// It sends two text messages and then closes the connection.
Core\Socket $conn = Core\Socket::current();
// `text()` returns the text exactly as the client sent it.
// This prints `hello (5 characters)`, then `good night (10 characters)`.
?Core\Socket\Message $message = $conn->receive();
while ($message != null) {
tainted string $text = $message->text() ?? '';
echo $text, ' (', Core\Str::length($text), " characters)\n";
$message = $conn->receive();
}
Output
hello (5 characters)
good night (10 characters)

A binary message has no text

<?nvs
// Shows that `text()` returns `null` for a binary message.
// The `.nvsp` file beside this example plays the client.
// It sends a text message, then a binary message.
Core\Socket $conn = Core\Socket::current();
// This prints `text: ping`, then `no text: this is a binary message`.
?Core\Socket\Message $message = $conn->receive();
while ($message != null) {
?tainted string $text = $message->text();
if ($text == null) {
echo "no text: this is a binary message\n";
} else {
echo 'text: ', $text, "\n";
}
$message = $conn->receive();
}
Output
text: ping
no text: this is a binary message

Reading chat commands

<?nvs
// Shows a chat connection that reads commands from the text the client sends.
// The `.nvsp` file beside this example plays the client.
// It picks a name, sends a chat line, and then types `/quit`.
Core\Socket $conn = Core\Socket::current();
tainted string $name = 'guest';
// A text that starts with `/` is a command. Any other text is a chat line.
// Each `sent:` line in the output is a message the client received.
?Core\Socket\Message $message = $conn->receive();
while ($message != null) {
tainted string $text = Core\Str::trim($message->text() ?? '');
if ($text == '/quit') {
$conn->send('goodbye, ' . $name);
break;
}
// `Core\Str::afterPrefix` returns the text after `/name `. For any other text, the result is `null`.
?tainted string $newName = Core\Str::afterPrefix($text, '/name ');
if ($newName != null) {
$name = $newName;
$conn->send('you are now ' . $name);
} else {
$conn->send($name . ': ' . $text);
}
$message = $conn->receive();
}
Output
sent: you are now Ana
sent: Ana: hello everyone
sent: goodbye, Ana