react/stream
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
evenement/evenement | v3.0.2 | 4.6 kB | MIT | prod | |
react/event-loop | v1.5.0 | 39.55 kB | MIT | prod |
React/Stream is a powerful PHP package that offers an API for working with readable and writable streams, allowing efficient processing of large amounts of data for non-blocking I/O. It facilitates the utilization of large data structures like multi-gigabyte file downloads without having to store everything in memory at once, providing an interface more suited for async, non-blocking I/O. This package is part of the ReactPHP project.
To utilize React/Stream, you first need to install the package via composer. Use the following command in your project directory:
composer require react/stream
After this, you can start working with the Stream package.
A basic usage example that writes 'Hello World' to stdout:
$loop = React\EventLoop\Factory::create();
$stream = new React\Stream\WritableResourceStream(fopen('php://stdout', 'w'), $loop);
$stream->write('Hello World');
$loop->run();
You can listen to events on the stream like 'data', 'end', 'error', and 'close':
$readable->on('data', function ($data) {
echo 'Received: ' . $data;
});
$readable->on('end', function () {
echo 'Connection ended';
});
$readable->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage();
});
$readable->on('close', function () {
echo 'Connection closed';
});
Piping between readable and writable streams is possible and it can be useful for cloning data or building processing chains:
$readable->pipe($writable);
Remember to always handle the stream events in order to effectively work with non-blocking I/O.
The documentation for React/Stream can be found within the package's GitHub repository. The README.MD file in the root of this repository serves as the main source of documentation and includes detailed information about streams, interfaces, and event semantics, as well as examples. For additional information about ReactPHP and related packages, you can refer to ReactPHP's website.