bodaclick/async-event-dispatcher
Async event dispatcher for PHP inspired by Symfony. Add one or more drivers (listeners) and dispatch AsyncEventInterface events using a fire-and-forget pub/sub style. Includes RabbitMQ and file drivers, with an easy interface for custom drivers.
Illuminate\Events\Dispatcher), offering a fire-and-forget alternative for async event handling. This is particularly useful for decoupling heavy or long-running tasks (e.g., notifications, analytics, external API calls) from the main request flow.Event facade for async use cases.dispatch(new Job)) but with event-driven semantics.EventDispatcher with minimal changes required (e.g., swapping Event::dispatch() for $asyncDispatcher->dispatch()).AsyncEventInterface (similar to Laravel’s ShouldBeStringable or custom interfaces), requiring minimal refactoring if existing events are already async-friendly.$ed->addDriver($driver)) fires it for all events, which could lead to unintended side effects (e.g., performance overhead, duplicate processing).Illuminate\Events\Dispatcher for async use cases.UserRegistered, OrderProcessed).AsyncEventInterface for these events.AsyncEventDispatcher in AppServiceProvider:
$this->app->singleton(AsyncEventDispatcher::class, function ($app) {
$dispatcher = new AsyncEventDispatcher();
$dispatcher->addDriver(new RabbitMQDriver(), 'event.name');
return $dispatcher;
});
Event::dispatch() with $dispatcher->dispatch().class QueueDriver implements AsyncEventDriverInterface {
public function handle(AsyncEventInterface $event) {
dispatch(new ProcessEventJob($event));
}
}
AsyncEventInterface. Add:
use BDK\AsyncEventDispatcher\AsyncEventInterface;
class UserRegistered implements AsyncEventInterface {
// ...
}
EventServiceProvider listeners (since they’re fire-and-forget). Design listeners to be idempotent.AsyncEventDispatcher in unit tests (e.g., using Mockery to verify events are dispatched to the driver).dispatcher->dispatch($event) logs event name + timestamp).php-amqplib) may require manual updates for security patches.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| RabbitMQ downtime | Events lost or delayed. | Fallback to synchronous dispatch or local queue. |
| Driver crashes | Events not processed. | Supervisor process to restart drivers. |
| Duplicate events | Idempotent operations required. | Use UUIDs or transaction IDs in events. |
| Consumer lag | Backlog of undelivered events. | Scale consumers or adjust prefetch limits. |
| Network partitions | Timeouts or failed dispatches. | Implement circuit breakers or retries. |
How can I help you explore Laravel packages today?