php8.5
Home/ Manual/ swoole / event/ Swoole\Event::write

Swoole\Event::write

PHP function Edit on GitHub ✎

(PECL swoole >= 1.9.0)

Write data to socket asynchronously

Description

Swoole\Event::write(mixed $fd, mixed $data): bool

Makes data sending asynchronous for stream/sockets resources.

Note

If data is continuously written to a socket but the peer cannot read fast enough, the socket buffer will eventually fill up. In this case, the Swoole underlying layer will store the excess data in an in-memory buffer and wait for the writable event to trigger before attempting to write to the socket again. However, if the in-memory buffer also becomes full, Swoole will throw a "pipe buffer overflow, reactor will block" error and switch to blocking mode, pausing further writes until space becomes available.

Note

The buffer-full return of false is an atomic operation—it guarantees either complete success (all data written) or total failure (nothing written).

Warning

Event::write cannot be used with SSL/TLS-encrypted streams or sockets (tunneled connections).

Warning

Upon successful execution, Event::write will automatically switch the $socket to non-blocking mode.

Parameters

fd

File descriptor (stream/socket resource, integer, or object).

data

Data to send (length must not exceed socket buffer size).

Return Values

Success

Examples

Swoole\Event::write() example

php
<?php
use Swoole\Event;

$fp = stream_socket_client('tcp://127.0.0.1:9501');
$data = str_repeat('A', 1024 * 1024*2);
Event::add($fp, function($fp) {
     echo fread($fp);
});
Event::write($fp, $data);
?>

Source: reference/swoole/swoole/event/write.xml · from the official PHP manual (php/doc-en)