Introduction
ReactPHP brings the event-driven programming model to PHP. Inspired by Node.js and Reactor pattern implementations, it provides a composable set of libraries for building async HTTP servers, clients, and stream processors in pure PHP.
What ReactPHP Does
- Provides an event loop for non-blocking I/O operations
- Includes an HTTP server and client for async web applications
- Offers stream abstractions for reading and writing data without blocking
- Supports async DNS resolution, filesystem access, and child processes
- Enables promise-based workflows for managing asynchronous results
Architecture Overview
ReactPHP is organized as a collection of small Composer packages centered around the event loop component. The loop uses the best available backend (ext-ev, ext-event, ext-uv, or a stream_select fallback). All I/O components — sockets, streams, HTTP, DNS — register callbacks with the loop and process data as it becomes available, enabling a single PHP process to handle many concurrent connections.
Self-Hosting & Configuration
- Install individual components via Composer (e.g.,
react/http,react/socket) - Start the event loop with
ReactEventLoopLoop::run() - Configure server listening address and port in your PHP script
- Tune OS limits (file descriptors, TCP backlog) for high concurrency
- Run as a persistent process using systemd or supervisord
Key Features
- Modular component design — use only what you need
- Promise-based async API following Promises/A+ semantics
- Built-in HTTP server capable of handling concurrent requests
- Stream processing with back-pressure support
- No C extensions required for basic functionality
Comparison with Similar Tools
- Swoole — coroutine-based C extension; ReactPHP is pure PHP with callback/promise patterns
- Workerman — multi-process PHP server; ReactPHP is a library toolkit rather than a server framework
- Amphp — similar async PHP library using fibers; ReactPHP uses promises and callbacks
- Node.js — JavaScript event-driven runtime; ReactPHP applies the same model to PHP
- Guzzle — synchronous HTTP client; ReactPHP's HTTP client is fully non-blocking
FAQ
Q: Can ReactPHP work with existing synchronous PHP code? A: Blocking calls will block the entire event loop. Use ReactPHP's async alternatives or run blocking code in child processes.
Q: Is ReactPHP production-ready? A: Yes. Its core components are stable and used in production for WebSocket servers, API gateways, and real-time applications.
Q: How does ReactPHP compare to PHP Fibers? A: PHP Fibers (8.1+) enable coroutine-style code. ReactPHP can use fibers internally while maintaining its promise-based public API.
Q: Does ReactPHP support WebSocket?
A: Yes. The react/socket and community packages like Ratchet provide WebSocket server capabilities.