ReactPHP is a low-level library for event-driven programming in PHP. It provides the foundation for asynchronous, non-blocking I/O operations, enabling PHP applications to handle multiple tasks concurrently without resorting to traditional multi-threading. At its core, ReactPHP revolves around an "Event Loop" – a central component that continuously checks for new events (like network data arriving, timers expiring, or files becoming readable/writable) and dispatches them to registered callbacks. This architecture allows a single PHP process to efficiently manage numerous simultaneous connections or operations, making it ideal for building high-performance network servers, real-time applications, and long-running processes.
Key components of ReactPHP include:
* Event Loop (`react/event-loop`): The heart of ReactPHP, responsible for scheduling and executing operations asynchronously. It dispatches I/O events, timers, and signals.
* Streams (`react/stream`): Abstractions over PHP's stream resources, providing an event-driven way to read from and write to non-blocking streams (like network sockets, pipes, and files).
* Promise (`react/promise`): An implementation of Promises/A+, a specification for asynchronous operations that may complete at some point in the future. It helps manage complex asynchronous workflows.
* Async (`react/async`): Helper functions for creating and working with async generators and coroutines, simplifying asynchronous code.
* Socket (`react/socket`): Low-level network abstractions for creating TCP/IP and UDP servers and clients.
* HTTP (`react/http`): A high-level HTTP server and client built on top of `react/socket` and `react/stream`, enabling the creation of fast, non-blocking web services.
ReactPHP is commonly used for:
* Real-time applications: Such as WebSocket servers for chat applications, live dashboards, or gaming.
* Network proxies and gateways: Efficiently routing and transforming network traffic.
* Long-running processes: Background workers, daemon processes, or message queue consumers that need to stay alive and react to events.
* Microservices: Building lightweight, high-performance network services.
* Command-line tools: Creating interactive or long-running CLI applications.
By adopting an event-driven model, ReactPHP helps overcome the traditional "shared-nothing" architecture limitations of PHP in web environments, allowing developers to write highly performant and resource-efficient applications that can handle a large number of concurrent connections with minimal overhead. It enables a more Node.js-like development style within the PHP ecosystem.
Example Code
<?php
require __DIR__ . '/vendor/autoload.php';
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\HttpServer;
use React\Socket\SocketServer;
$loop = Factory::create();
$http = new HttpServer($loop, function (ServerRequestInterface $request) {
// This callback is executed for every incoming HTTP request
// $request object contains details about the incoming request
$path = $request->getUri()->getPath();
if ($path === '/hello') {
return new \React\Http\Message\Response(
200,
['Content-Type' => 'text/plain'],
'Hello, ReactPHP!'
);
} elseif ($path === '/time') {
return new \React\Http\Message\Response(
200,
['Content-Type' => 'text/plain'],
'Current time: ' . date('Y-m-d H:i:s')
);
} else {
return new \React\Http\Message\Response(
404,
['Content-Type' => 'text/plain'],
'Not Found'
);
}
});
// Listen on port 8080
$socket = new SocketServer('0.0.0.0:8080', $loop);
$http->listen($socket);
echo "Server running at http://127.0.0.1:8080\n";
echo "Try http://127.0.0.1:8080/hello\n";
echo "Try http://127.0.0.1:8080/time\n";
$loop->run();
?>








reactphp/reactphp