Swoole\Server->send()

Declaration

<?php
Swoole\Server->send(int $fd, string $data, int $serverSocket  = -1): bool

Parameters

fd

The file descriptor of the client to send data to

data

The data you want to send back to the client

serverSocket

The Unix socket address for when you are using `UnixSocket DGRAM, not required when using just TCP

Return

success

If success, it returns 'true', otherwise it returns false.

Description

Send data back to the client.

Because the send operation is atomic, multiple processes that call send to the same TCP connection, they will not conflict with each other.

Data Length

The max TCP data size is 2MB. This limit can be changed by configuring the configuration of buffer_output_size.

If the buffer is reached, Swoole will write to a temporary file until the buffer is free enough to use again.

When using a UnixSocket, if the buffer becomes full, a temporary file is also used until the buffer is free again. If the buffer is not available for a long period of time, it can lead to a socket memory buffer overflow, Swoole will then immediately return false and you can then save the data to disk and send again when the buffer is not full.

When coroutines are enabled and a server worker is sending data , the coroutine will yield when the buffer is full automatically to allow other processes to continue but will continue execution once the buffer is free again, allowing the server to operate more concurrently.

Handling Send Errors

If the result is false, you can call the method $server->getLastError() to get the last error code.

Example

Listening using UnixSocket DGRAM and sending data to the peer:

<?php
$server->on("packet", function (Swoole\Server $server, $data, $addr)
{
    $server->send($addr['address'], 'SUCCESS', $addr['server_socket']);
});