Core\Http\Socket::receive
$socket->receive(): ?Core\Socket\MessageWaits for the next message from the server on a WebSocket connection your program opened, and returns it.
The result is a Core\Socket\Message. A text message has its content in text(), and a binary message has it in bytes(). The other of the two is null. Both are tainted, because the content came from outside your program. receive() returns null when the conversation is over: the server closed the connection, or your program called close().
receive() waits, but not forever. If the server sends nothing for longer than the idle time, it throws a TimeoutError. A message larger than maxMessage closes the connection and throws a RuntimeError.
The examples below read every message until the end, tell text and binary messages apart, and answer each request a server sends.
Return value
Returns ?Core\Socket\Message. A Core\Socket\Message. Its text() or bytes() returns the content, which is tainted because it comes from the network. Its topic() and value() are null, because only a message from another isolate sets them. null means the connection is closed, by the peer or by your program.
Errors
TimeoutErrorThe peer sent nothing for longer thanidle, or the socket'smaxDurationended while this call was waiting. A peer that sends only pings is not silent, so onlymaxDurationlimits it.IOErrorThe connection failed while this call was waiting for a message.RuntimeErrorThe peer sent a message larger thanmaxMessage. The socket is closed with code1009before the error is thrown. This stops the peer from filling the task's memory with a message that nobody reads.LogicErrorAnotherreceive()is already waiting on this socket. Each message goes to one receiver, so only the task that owns the socket reads from it.
Examples
Reading every message
<?nvs// Shows how to read every message from a server until the server closes the connection.
// A fixed server, so the example runs without a network. It sends three messages and then closes.Core\Test::answerSocket('wss://news.example.com/live', ['rain today', 'match starts at 8', 'shop opens late']);
Core\Http\Socket $socket = Core\Http\Client::openSocket('wss://news.example.com/live');
// `receive()` returns `null` when the server has closed, so the loop stops there.// This prints the three messages, then `3 messages`.uint $count = 0;?Core\Socket\Message $message = $socket->receive();while ($message != null) { echo 'news: ', $message->text() ?? '', "\n"; $count = $count + 1; $message = $socket->receive();}echo $count, " messages\n";news: rain todaynews: match starts at 8news: shop opens late3 messagesText and binary messages
<?nvs// Shows how to tell a text message from a binary message.
// A fixed server, so the example runs without a network.// It sends one text message and one binary message of four bytes.Core\Test::answerSocket('wss://camera.example.com/frames', ['frame 1', 'JPEG' as bytes]);
Core\Http\Socket $socket = Core\Http\Client::openSocket('wss://camera.example.com/frames');?Core\Socket\Message $message = $socket->receive();while ($message != null) { // A text message has `text()`. A binary message has `bytes()`, and its `text()` is `null`. ?tainted string $text = $message->text(); if ($text != null) { echo 'text: ', $text, "\n"; } else { // This prints `binary: 4 bytes`. echo 'binary: ', Core\Bytes::length($message->bytes() ?? ('' as tainted bytes)), " bytes\n"; } $message = $socket->receive();}text: frame 1binary: 4 bytesAnswering a servers requests
<?nvs// Shows a worker that receives jobs from a server and sends back an answer for each one.
class Worker{ // Receives messages until the server closes. Answers `ping` with `pong` // and every other message with `done:` and the job name. public static function run(Core\Http\Socket $socket): uint { uint $jobs = 0; ?Core\Socket\Message $message = $socket->receive(); while ($message != null) { tainted string $job = $message->text() ?? ('' as tainted string); if ($job == 'ping') { $socket->send('pong'); } else { $socket->send('done:' . $job); $jobs = $jobs + 1; } $message = $socket->receive(); } return $jobs; }}
// A fixed server, so the example runs without a network.Core\Test::answerSocket('wss://jobs.example.com/queue', ['resize-image', 'ping', 'send-invoice']);
Core\Http\Socket $socket = Core\Http\Client::openSocket('wss://jobs.example.com/queue');// This prints `2 jobs`.echo Worker::run($socket), " jobs\n";
// `Core\Test::sentSocket()` returns what the program sent. This prints the three answers.foreach (Core\Test::sentSocket() as Core\Socket\Message $sent) { echo 'sent: ', $sent->text() ?? '', "\n";}2 jobssent: done:resize-imagesent: pongsent: done:send-invoice