🐘 PHP Advanced

What is the PHP Event Loop concept with ReactPHP?

Answer

Traditional PHP-FPM handles one request per process — it is synchronous and blocking. ReactPHP's event loop enables a single PHP process to handle thousands of concurrent I/O operations asynchronously. The event loop monitors file descriptors, timers, and signals. When an I/O operation (network request, file read) is ready, the loop dispatches the registered callback — no blocking. Key ReactPHP components: EventLoop (the core), Socket (async TCP/UDP), Http (async HTTP server), ChildProcess (spawn processes), Promise (deferred values). Example: a ReactPHP HTTP server handles 10,000 concurrent connections in a single PHP process, while PHP-FPM would need 10,000 worker processes. ReactPHP is ideal for WebSocket servers, API gateways, and long-running background services where PHP-FPM's process-per-request model is too expensive.