enqueue/async-event-dispatcher
Symfony EventDispatcher extension that dispatches events asynchronously by sending them to a message queue, enabling background processing and improved responsiveness. Part of the Enqueue ecosystem; integrates with Symfony apps and supports MQ-driven event handling.
Installation
composer require enqueue/async-event-dispatcher
Requires php-enqueue/amqp-ext or php-enqueue/amqp-bunny for AMQP support.
Basic Configuration
Add to your Laravel config/services.php:
'async_event_dispatcher' => [
'dsn' => env('ASYNC_DISPATCHER_DSN', 'amqp://guest:guest@localhost:5672/%2f'),
'queue' => env('ASYNC_DISPATCHER_QUEUE', 'async_events'),
'connection' => env('ASYNC_DISPATCHER_CONNECTION', 'default'),
],
First Use Case
Replace Laravel’s default dispatcher in AppServiceProvider:
use Enqueue\AsyncEventDispatcher\AsyncEventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcher;
public function register()
{
$dispatcher = new AsyncEventDispatcher(
new EventDispatcher(),
$this->app['enqueue.client']
);
$this->app->singleton('event', fn() => $dispatcher);
}
Verify Dispatch an event and check the AMQP queue for messages:
php artisan tinker
>>> event(new \App\Events\TestEvent);
Event Listeners Register listeners as usual, but they’ll execute asynchronously:
Event::listen(MyEvent::class, function ($event) {
// Runs in background
});
Priority Queues Use custom queues for critical events:
$dispatcher->dispatch(new UrgentEvent(), 'urgent_queue');
Error Handling Wrap dispatch calls in try-catch:
try {
event(new PaymentProcessed($user));
} catch (\Enqueue\Client\Exception\ConnectionException $e) {
Log::error('Async dispatch failed', ['exception' => $e]);
// Fallback to sync dispatch
}
Laravel Queues Combine with Laravel’s queue system for hybrid workflows:
$dispatcher->dispatch(new JobSubmitted($job), 'jobs');
Middleware
Apply middleware to async events via AsyncEventDispatcher constructor:
$dispatcher = new AsyncEventDispatcher(
$dispatcher,
$client,
[$middleware1, $middleware2]
);
Testing Mock the AMQP client for unit tests:
$mockClient = Mockery::mock(ClientInterface::class);
$dispatcher = new AsyncEventDispatcher($dispatcher, $mockClient);
Connection Issues
ConnectionException handling.Duplicate Events
Event::unique() (if supported).Ordering Guarantees
Memory Leaks
ClientInterface is properly closed (e.g., in Laravel’s terminate()).Check Queue
Use php-amqpcli or RabbitMQ Management UI to inspect the queue:
php-amqpcli get queue async_events
Log Dispatches
Enable debug logging in config/logging.php for enqueue channel.
Custom Serializer
Override the default EventSerializer for complex event payloads:
$dispatcher = new AsyncEventDispatcher(
$dispatcher,
$client,
null,
new CustomEventSerializer()
);
Dynamic Queue Routing
Implement QueueNameResolverInterface to route events dynamically:
$dispatcher->dispatch($event, $resolver->resolve($event));
Consumer Integration
Extend the AsyncEventDispatcher to integrate with Laravel’s queue workers:
// In a custom worker class
$dispatcher->consume($queue, fn($event) => $this->handle($event));
How can I help you explore Laravel packages today?