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

Latest version: pecl install openswoole-22.1.2


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



fd of the socket type


Data to send to the socket, must not exceed the length of the transmission data buffer size



If success, it returns TRUE, meaning the event listener was successfully added

Otherwise it returns FALSE, meaning there was an issue registering the event


Write data to the socket with async I/O, by using Event::write you can achieve asynchronous data transmission, once the data buffer is full, a EAGAIN error will be returned and the data waiting for a read event will be added to the transmission queue, once the socket is writeable again, Swoole will automatically listen for the write event again.

When using Event:write, if the operation is successful, the event loop automatically switches the socket to non-blocking mode. You also should not use Event::write with SSL/TLS connections.

Under the hood, Swoole will allow you to write as long as the buffer is not full, if it is full, Swoole will write the extra data to memory and save it until a writeable event is triggered and the buffer becomes free again. If the memory buffer is also filled, Swoole will then throw a pipe buffer overflow error and block the event loop waiting to write again.



$fp = stream_socket_client('tcp://');
$data = str_repeat('A', 1024 * 1024 * 2);

// Create the read callback
Swoole\Event::add($fp, function($fp) {
     echo fread($fp);

// Write data to the socket
Swoole\Event::write($fp, $data);
Last updated on August 31, 2022