Core\Socket::send
Sends one text message to the client on the WebSocket connection your script is running for.
A connection script gets the connection with Core\Socket::current() and calls send() on it. The client receives the whole string as one message. send() returns nothing. You may send a tainted string, for example a message the client sent you. To send binary data, use sendBytes().
send() waits until the message is written, but not forever. If the client stops reading and the message cannot be written in time, send() throws a RuntimeError. Outside a connection, it throws a LogicError.
The examples below answer each message, handle a client that stopped reading, and forward the messages of a chat room to the client.
Parameters
$framestringThe text to send. The client receives it as one message. Ataintedstring is allowed, so you can send back what the client sent you.
Return value
Returns void. Nothing.
Errors
LogicErrorThe script is not running for a connection, so there is no client to send to.RuntimeErrorThe connection broke, or the client stopped reading and the message could not be written in time. The wait always ends.
Examples
Answering each message
<?nvs// Shows a connection that answers every message the client sends.
// The `.nvsp` file beside this example plays the client.// It sends two messages and then closes the connection.Core\Socket $conn = Core\Socket::current();
// Each `sent:` line in the output is a message the client received.// This prints `sent: you said: hello` and `sent: you said: how are you?`.?Core\Socket\Message $message = $conn->receive();while ($message != null) { $conn->send('you said: ' . ($message->text() ?? '')); $message = $conn->receive();}echo "the client closed the connection\n";sent: you said: hellosent: you said: how are you?the client closed the connectionWhen the client stops reading
<?nvs// Shows what happens when the client stops reading the messages you send.
// The `.nvsp` file beside this example plays a client that reads two messages and then stops.Core\Socket $conn = Core\Socket::current();
// `send()` throws a `RuntimeError` when a message cannot be written in time.// This prints two `sent:` lines, then the error, then `stopped after 2 messages`.uint $sent = 0;try { while ($sent < 5) { $conn->send('update number ' . (($sent + 1) as string)); $sent = $sent + 1; }} catch (RuntimeError $e) { echo 'error: ', $e->message, "\n";}echo 'stopped after ', $sent, " messages\n";sent: update number 1sent: update number 2error: `Core\Socket::send` failed on the connection: the client stopped reading, and the send wait expiredstopped after 2 messagesForwarding a chat room
<?nvs// Shows a chat connection that sends every message posted in a room to its client.
Core\Socket $conn = Core\Socket::current();Core\Topic::subscribe('room:lobby');
// Other connections post to the room. Here the script posts two messages itself,// so the example runs on its own.Core\Topic::publish('room:lobby', ['from' => 'ana', 'text' => 'hi all']);Core\Topic::publish('room:lobby', ['from' => 'ben', 'text' => 'hello ana']);
// `receive()` returns the two room messages, then `null` when the client closes.// Each room message goes to the client as JSON text.?Core\Socket\Message $message = $conn->receive();while ($message != null) { if ($message->topic() != null) { $conn->send(Core\Json::encode($message->value())); } $message = $conn->receive();}sent: {"from":"ana","text":"hi all"}sent: {"from":"ben","text":"hello ana"}