Swoole Server sendwait

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\Server->sendwait(int $fd, string $data): bool

Parameters

fd

The file descriptor of the client to send data to

data

The data to send to the client

Return

success

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

Description

Send data to the remote socket in a synchronously blocking way.

There may be some situations where a server needs to send data to the client continuously and needs to wait for that operation to finish before continuing on. When using the normal $server->send method, it is purely a asynchronous operation and data is stored in memory when sending, using a buffer to queue data, which can lead to a buffer overflow.

By using $server->sendwait it can solve the buffer overflow problem, sendwait will wait for the connection to be completely finished before moving on, it will not return until all the data has been sent.

Only available in SWOOLE_BASE mode. Only a blocking server should use this function, do not use this with coroutines.

Example

<?php
$server->on('receive', function(Swoole\Server $server, $fd, $reactorId, $data)
{
    // Send data back to the client
    $server->sendwait($fd, data);
});
Last updated on August 31, 2022