react/event-loop
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
React/Event-Loop is a core component of ReactPHP's suite of tools aimed at handling asynchronous I/O operations. It serves as a conduit for interoperability, allowing various asynchronous libraries to utilize the same event loop. This makes managing event-driven I/O operations significantly easier and more efficient. React/Event-Loop provides a common LoopInterface that can be targeted by any library, enabling them to run harmoniously within a single event loop.
To use react/event-loop, you'll need to include it in your PHP project. If you are familiar with Composer, you can easily install ReactPHP's EventLoop by running the command composer require react/event-loop
. After the installation, you can use it with PHP bundling tools to take advantage of its functionality in your web application.
Here is a simple quickstart usage example:
<?php
use React\EventLoop\Loop;
require __DIR__ . '/vendor/autoload.php';
$server = stream_socket_server('tcp://127.0.0.1:8080');
stream_set_blocking($server, false);
Loop::addReadStream($server, function ($server) {
$conn = stream_socket_accept($server);
$data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
Loop::addWriteStream($conn, function ($conn) use (&$data) {
$written = fwrite($conn, $data);
if ($written === strlen($data)) {
fclose($conn);
Loop::removeWriteStream($conn);
} else {
$data = substr($data, $written);
}
});
});
Loop::addPeriodicTimer(5, function () {
$memory = memory_get_usage() / 1024;
$formatted = number_format($memory, 3).'K';
echo "Current memory usage: {$formatted}\n";
});
This code showcases the creation of an asynchronous HTTP server with react/event-loop. To begin with, stream_socket_server
is used to create the server socket and set it to non-blocking mode. A stream is then added to the event loop with Loop::addReadStream
. This script also establishes a periodic timer that shows the current memory usage every five seconds.
The official documentation for react/event-loop is available on the project's GitHub page (https://github.com/reactphp/event-loop). The readme file provides a detailed overview of the package's functionality, including how to use it for building server-side applications, handling signals and timers, and working with stream and future ticks. It also gives a rundown of different LoopInterface methods such as addTimer
, addReadStream
, addWriteStream
, and several others. Beginners and experienced developers alike will find the docs to be a thorough guide for incorporating react/event-loop into their PHP projects.