- Can I use Sabre/Event for Laravel HTTP requests (e.g., controllers, middleware)?
- No, Sabre/Event’s EventLoop should never run in HTTP contexts—it blocks Laravel’s request lifecycle. Reserve it for CLI workers, Artisan commands, or queue jobs where non-blocking execution is safe. For HTTP async tasks, use Laravel queues or Swoole/RoadRunner instead.
- How do I integrate Sabre/Event with Laravel’s service container?
- Sabre/Event isn’t natively bound to Laravel’s container. Manually instantiate `Sabre\Event\Emitter` or `Sabre\Event\EventLoop` in service providers or custom classes. For consistency, wrap the Emitter in a facade or service class that extends Laravel’s `EventServiceProvider` patterns.
- What’s the difference between Sabre/Event and Laravel’s built-in Events/Bus?
- Laravel’s Events/Bus are synchronous and HTTP-friendly, while Sabre/Event adds async primitives: Promises (`.then()`), an EventLoop for non-blocking I/O, and coroutines (PHP 8.2 generators). Use Sabre/Event for CLI tools, WebSocket servers, or stateful async workflows where Laravel’s sync system is limiting.
- Will Sabre/Event work with Laravel 9 or PHP 8.1?
- No, Sabre/Event requires PHP 8.2+. For Laravel 9 (PHP 8.1), use version 6.0.x (PHP 7.4–8.1) of Sabre/Event, but note it lacks coroutines and may have Promise API differences. Plan to upgrade to PHP 8.2+ for full feature support, including coroutines and stricter typing.
- How do I handle errors in Sabre/Event coroutines (PHP 8.2 generators)?
- Coroutines throw exceptions like regular code, but stack traces can be harder to debug. Use `try/catch` blocks around `yield` expressions and log errors with context. For production, implement a custom error handler for coroutine-specific exceptions or wrap coroutines in a `Promise` to centralize error handling.
- Can I use Sabre/Event’s EventEmitter with Laravel’s event listeners?
- Yes, but manually. Subscribe to events via `emitter->on('event.name', fn() => ...)` and dispatch with `emitter->emit('event.name')`. For Laravel integration, bind the Emitter to a service and use it alongside `event(new MyEvent())` where async behavior is needed, but avoid mixing both systems in critical paths.
- What’s the performance impact of Sabre/Event vs. ReactPHP or Laravel queues?
- Sabre/Event’s EventLoop is lightweight for PHP 8.2+ but may introduce latency compared to synchronous Laravel queues. Benchmark against ReactPHP for high-throughput I/O (e.g., WebSockets) or use Laravel queues for simple async tasks. Sabre/Event shines in CLI tools or coroutine-heavy workflows where ReactPHP’s overhead isn’t justified.
- How do I test Sabre/Event in a Laravel project?
- Test in isolation by mocking the Emitter/EventLoop in unit tests. For integration tests, use Artisan commands or queue jobs to trigger async logic. Avoid testing EventLoop in HTTP tests—simulate async behavior with Promises or mock the loop’s `run()` method. Use PHPUnit’s `expectException` for Promise rejection tests.
- Are there alternatives to Sabre/Event for async Laravel apps?
- For HTTP async, use Laravel queues or Swoole/RoadRunner. For CLI async, ReactPHP is more mature but heavier. If you only need events, Laravel’s built-in system suffices. Sabre/Event is unique for its Promise + EventLoop combo and coroutines, but opt for it only if you need PHP 8.2’s async features without Swoole’s complexity.
- How do I migrate from Sabre/Event v6.0 (PHP 7.4–8.1) to v6.1 (PHP 8.2+)?
- Upgrade PHP to 8.2 first, then update Composer to `sabre/event:^6.1`. Check for Promise API changes (e.g., `Promise::error` → `Promise::otherwise`) and update coroutine code to use PHP 8.2’s `yield` syntax. Test thoroughly—v6.1 introduces stricter typing and removes legacy PHP 7.x features. Refer to the [migration guide](https://sabre.io/event/migration/) for details.