Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-26.11.1 | composer require openswoole/core:26.11.1
<?php OpenSwoole\Server->on('BufferFull', Callable $callback)
The event callback name.
Callable event function, called as function (OpenSwoole\Server $server, int $fd).
If success, it returns true, otherwise it returns false.
Execute the callback function when the send buffer of a connection reaches the high watermark, set with the buffer_high_watermark option (by default 80% of socket_buffer_size).
The event is called once for each time the buffer reaches the high watermark. While the buffer is full, the application should stop calling $server->send() for the connection, as more data may fail with an output buffer overflow error. Continue sending after the BufferEmpty event.
The callback receives the server object and the session ID $fd of the connection.
In OpenSwoole\Server::POOL_MODE this event can not be used with dispatch_mode 1, 3 or 7 unless enable_unsafe_event is set, the callback is ignored with a warning.
<?php
$server = new OpenSwoole\Server("127.0.0.1", 9501);
$server->set([
'socket_buffer_size' => 8 * 1024 * 1024,
'buffer_high_watermark' => 4 * 1024 * 1024,
]);
$paused = [];
$server->on('BufferFull', function(OpenSwoole\Server $server, int $fd) use (&$paused)
{
$paused[$fd] = true;
echo "Send buffer of connection #{$fd} is full\n";
});
$server->on('BufferEmpty', function(OpenSwoole\Server $server, int $fd) use (&$paused)
{
unset($paused[$fd]);
echo "Send buffer of connection #{$fd} is empty\n";
});
$server->on('Receive', function(OpenSwoole\Server $server, int $fd, int $reactorId, string $data) use (&$paused)
{
if (empty($paused[$fd])) {
$server->send($fd, $data);
}
});
$server->start();