Event Loops with ReactPHP
Run non-blocking I/O with the ReactPHP event loop.
ReactPHP in One Sentence
ReactPHP is a pure-PHP, dependency-free toolkit for non-blocking I/O built around a single event loop. No extension required — it works on stock PHP using stream_select (or ext-event/ext-ev when available). With it you write long-running servers, parallel HTTP clients, and streaming pipelines in plain PHP.
The Global Loop
Modern ReactPHP (v1.2+) exposes a default loop via the static React\EventLoop\Loop facade — no need to pass a loop instance everywhere. Schedule a callback with a timer; the loop runs until it has no more work.
<?php
require 'vendor/autoload.php';
use React\EventLoop\Loop;
Loop::addTimer(1.0, function () {
echo "fired after 1 second\n";
});
echo "scheduled\n";
// Loop::run() is auto-invoked at script shutdown in v1.2+All lessons in this course
- The PHP Concurrency Model
- PHP 8.1 Fibers
- Event Loops with ReactPHP
- High-Performance Servers with Swoole