Core\Socket::receive
$socket->receive(): ?Core\Socket\MessageWaits for the next message on the WebSocket connection your script is running for, and returns it.
A message comes from one of two places. The client sent it, or another part of your application published it to a topic this connection subscribed to. Call topic() on the message to tell them apart: it is null for a message from the client. receive() returns null when the client closes the connection, so a connection script is one loop that stops at null.
What the client sent is tainted, because it is untrusted input. If the connection breaks, receive() throws a RuntimeError.
Good to know: a connection that falls too far behind its topics is closed, and then receive() returns null.
The examples below read every message, tell text from binary messages, and run a chat connection that reads from its client and from a room with one loop.
Return value
Returns ?Core\Socket\Message. The next message, or null when the client has closed the connection. A connection script reads in a loop that stops at null. topic() on the message is null for a message from the client, and what the client sent is tainted. A connection that falls too far behind its topics is closed, and then this returns null too.
Errors
LogicErrorThe script is not running for a connection, so there is no client to wait for.ParseErrorA published object has a class that this program does not declare, or its properties do not match that class. The error names the topic and the class. The message is removed, so the next call returns the next message.RuntimeErrorThe connection broke, or the client sent something the WebSocket protocol does not allow.
Examples
Reading every message
<?nvs// Shows how to read every message from the client until the client closes the connection.
// The `.nvsp` file beside this example plays the client.// It sends three messages and then closes the connection.Core\Socket $conn = Core\Socket::current();
// `receive()` returns `null` when the client has closed, so the loop stops there.// This prints the three messages, then `3 messages`.uint $count = 0;?Core\Socket\Message $message = $conn->receive();while ($message != null) { echo 'client: ', $message->text() ?? '', "\n"; $count = $count + 1; $message = $conn->receive();}echo $count, " messages\n";client: helloclient: is anybody there?client: bye3 messagesText and binary messages
<?nvs// Shows how to tell a text message from a binary message.
// The `.nvsp` file beside this example plays the client.// It sends a text message, a binary message and another text message.Core\Socket $conn = Core\Socket::current();
// A message has either text or bytes. The other method returns `null`.// This prints `text: hi`, then `binary: 4 bytes`, then `text: done`.?Core\Socket\Message $message = $conn->receive();while ($message != null) { ?tainted bytes $octets = $message->bytes(); if ($octets != null) { echo 'binary: ', Core\Bytes::length($octets), " bytes\n"; } else { echo 'text: ', $message->text() ?? '', "\n"; } $message = $conn->receive();}text: hibinary: 4 bytestext: doneA chat connection
<?nvs// Shows a chat connection that reads from its client and from a chat room with one loop.
// The `.nvsp` file beside this example plays a client that sends two chat lines and then closes.Core\Socket $conn = Core\Socket::current();Core\Topic::subscribe('room:lobby');
// This prints two `sent:` lines, one for each chat line, as JSON text.?Core\Socket\Message $message = $conn->receive();while ($message != null) { if ($message->topic() == null) { // A message from the client has no topic. It is posted to the room, // so every connection in the room receives it, this one too. Core\Topic::publish('room:lobby', ['text' => $message->text() ?? '']); } else { // A message from the room goes to the client. $conn->send(Core\Json::encode($message->value())); } $message = $conn->receive();}echo "the client left the room\n";sent: {"text":"good morning"}sent: {"text":"who is here today?"}the client left the room