revolt/event-loop
Revolt is a rock-solid event loop for concurrent PHP 8.1+ apps using fibers. It enables non-blocking I/O with synchronous code, serving as a minimal, shared scheduler base for libraries like Amp and ReactPHP.
Installation:
composer require revolt/event-loop
Requires PHP 8.1+.
Basic Event Loop Initialization:
use Revolt\EventLoop\EventLoop;
$loop = EventLoop::get();
$loop->run();
First Use Case: Non-blocking HTTP Requests
$suspension = EventLoop::getSuspension();
$response = $suspension->suspend(fn() => file_get_contents('https://example.com'));
echo $response;
EventLoop Class – Central interface for managing the loop lifecycle, suspensions, and error handling.EventLoop::getSuspension() to yield control back to the loop.$suspension = EventLoop::getSuspension();
$data = $suspension->suspend(fn() => fetchDataFromDatabase());
// Process `$data` while other fibers run.
$loop = EventLoop::get();
$timer = $loop->addTimer(1.0, fn() => echo "Task completed after 1 second\n");
setErrorHandler.EventLoop::get()->setErrorHandler(fn(Throwable $e) => error_log($e->getMessage()));
$fiberLocal = new FiberLocal();
$fiberLocal->set('request_id', uniqid());
// Accessible in other fibers via `$fiberLocal->get('request_id')`.
// In a service provider:
public function boot()
{
$loop = EventLoop::get();
$loop->addTimer(5.0, fn() => $this->scheduleCleanup());
}
sleep() with $loop->addTimer().file_get_contents, DB queries) in suspensions.revolt/uv), install via:
pecl install revolt-uv
composer require revolt/uv
Unresumed Suspensions
try-finally:
$suspension = EventLoop::getSuspension();
try {
$result = $suspension->suspend(fn() => blockingCall());
} finally {
$suspension->resume();
}
Fiber Destruction Order
Callback Ordering
EventLoop::queue() for microtasks or addTimer for delayed execution.Serialization Warnings
EventLoop instances.PCNTL Dependencies
uv) require pcntl extensions.pcntl or use the select driver as a fallback.EventLoop::setErrorHandler to log uncaught exceptions with stack traces.EventLoop::getSuspension()->getStackTrace() to debug hanging fibers.print_r(EventLoop::getAvailableDrivers());
Custom Drivers
Revolt\EventLoop\Driver\DriverInterface for custom I/O backends.Decorators
$loop = new Decorator(EventLoop::get(), new LoggingMiddleware());
Fiber Hooks
Fiber::start() or Fiber::suspend() for custom behavior.$this->app->singleton(EventLoop::class, fn() => EventLoop::get());
$loop = EventLoop::get();
$loop->addTimer(60, fn() => $this->processQueue());
$loop->run();
How can I help you explore Laravel packages today?