Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Async Event Dispatcher Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Event-Driven Fit: The package bridges Symfony’s synchronous EventDispatcher with asynchronous message queues (e.g., RabbitMQ, Redis, Amazon SQS), aligning well with architectures requiring decoupled event processing (e.g., microservices, background jobs, or high-throughput systems).
  • Laravel Compatibility: Laravel’s native Event system (via Illuminate\Events\Dispatcher) is a drop-in replacement for Symfony’s EventDispatcher, making this package a natural fit for offloading event listeners to queues.
  • Use Cases:
    • Performance: Offload heavy event listeners (e.g., analytics, notifications) to avoid blocking HTTP responses.
    • Resilience: Isolate event processing failures from the main application.
    • Scalability: Distribute event workloads across workers.

Integration Feasibility

  • Core Integration:
    • Replace Laravel’s EventDispatcher with AsyncEventDispatcher (via enqueue/async-event-dispatcher).
    • Configure a message broker (e.g., RabbitMQ, Redis) as the transport layer.
    • Use Laravel’s queue:work or a dedicated worker process (e.g., Supervisor) to process async events.
  • Laravel-Specific Considerations:
    • Service Provider Binding: Override Laravel’s default EventDispatcher binding in AppServiceProvider.
    • Queue Configuration: Ensure the broker is configured in .env (e.g., QUEUE_CONNECTION=redis).
    • Event Serialization: Events must be serializable (e.g., implement JsonSerializable or use Illuminate\Contracts\Support\Arrayable).
  • Dependencies:
    • Requires php-enqueue/amqp-ext or php-enqueue/redis for transport (adds ~10MB to deployment).
    • No native Laravel queue support; relies on enqueue/amqp-ext or enqueue/redis for queue drivers.

Technical Risk

Risk Area Description Mitigation Strategy
Event Serialization Non-serializable events (e.g., closures, resources) will fail silently. Enforce JsonSerializable or Arrayable in event contracts.
Broker Dependency Tight coupling to RabbitMQ/Redis; may require additional infrastructure. Use Laravel’s QUEUE_CONNECTION to abstract the broker (e.g., switch to database).
Error Handling Failed async events may go unnoticed (no built-in retry/dead-letter queue). Implement a failed_jobs table or use Laravel’s ShouldQueue with retries.
Laravel Version Last release in 2017; may not support Laravel 10+ features (e.g., PSR-15). Fork or patch for compatibility; monitor for updates.
Testing Complexity Async events require mocking queues/workers in unit tests. Use Queue::fake() or Queue::assertPushed() for testing.

Key Questions

  1. Broker Choice:
    • Is RabbitMQ/Redis already in use, or will this require new infrastructure?
    • Are there compliance/latency constraints that favor one broker over another?
  2. Event Criticality:
    • Are all events fire-and-forget, or do some require acknowledgment/retry logic?
  3. Laravel Version:
    • Is the package compatible with the target Laravel version (e.g., 8/9/10)?
  4. Monitoring:
    • How will async event failures be monitored (e.g., Sentry, Laravel Horizon)?
  5. Fallback Strategy:
    • Should synchronous events be used as a fallback for critical paths?

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Native Integration: Works seamlessly with Laravel’s Event system, queue:work, and ShouldQueue jobs.
    • Queue Drivers: Leverages Laravel’s queue configuration (e.g., QUEUE_CONNECTION=redis).
    • Worker Processes: Uses Laravel’s built-in queue:work or custom workers (e.g., Supervisor).
  • Broker Compatibility:
    • Supported: RabbitMQ (via php-enqueue/amqp-ext), Redis (via php-enqueue/redis).
    • Unsupported: Database, SQS, or custom drivers (would require wrapper layer).
  • Alternatives:
    • Laravel Horizon: For more advanced queue monitoring (but adds complexity).
    • Symfony Messenger: Modern alternative with better retry/transport support.

Migration Path

  1. Phase 1: Proof of Concept (PoC)
    • Replace EventDispatcher in a single module (e.g., notifications).
    • Test with a non-critical event (e.g., Sent event for emails).
    • Verify serialization and queue processing.
  2. Phase 2: Broker Setup
    • Configure Redis/RabbitMQ and update .env.
    • Set up a worker process (queue:work or Supervisor).
  3. Phase 3: Gradual Rollout
    • Migrate high-impact events first (e.g., analytics, reports).
    • Monitor failure rates and performance.
  4. Phase 4: Full Adoption
    • Replace all synchronous event listeners with async variants.
    • Deprecate old synchronous listeners.

Compatibility

Component Compatibility Notes
Laravel Events Full compatibility; drop-in replacement for EventDispatcher.
Queue Drivers Requires enqueue/amqp-ext or enqueue/redis (not native Laravel drivers).
Event Listeners Must be serializable (e.g., no closures, resources).
Middleware Async listeners cannot use Symfony middleware (not supported by this package).
Testing Use Queue::fake() for unit tests; mock workers for integration tests.

Sequencing

  1. Broker Setup:
    • Install and configure Redis/RabbitMQ.
    • Update composer.json:
      "require": {
        "php-enqueue/async-event-dispatcher": "^1.0",
        "php-enqueue/redis": "^0.10" // or "php-enqueue/amqp-ext"
      }
      
  2. Service Provider:
    • Bind AsyncEventDispatcher in AppServiceProvider:
      use Enqueue\AsyncEventDispatcher\AsyncEventDispatcher;
      use Enqueue\Client\Producer;
      
      public function register()
      {
          $producer = new Producer(new \Enqueue\Redis\RedisConnection());
          $this->app->bind(\Symfony\Component\EventDispatcher\EventDispatcherInterface::class,
              function () use ($producer) {
                  return new AsyncEventDispatcher($producer, new \Symfony\Component\EventDispatcher\EventDispatcher());
              });
      }
      
  3. Event Configuration:
    • Annotate listeners with @Async (if supported) or ensure they’re serializable.
    • Example:
      class SendNotificationListener implements ShouldQueue
      {
          public function handle(ExampleEvent $event)
          {
              // Async processing
          }
      }
      
  4. Worker Setup:
    • Start workers:
      php artisan queue:work --queue=events
      
    • Or use Supervisor for production:
      [program:laravel-worker]
      command=php artisan queue:work --queue=events
      numprocs=8
      

Operational Impact

Maintenance

  • Dependencies:
    • Broker Maintenance: Redis/RabbitMQ requires monitoring (e.g., memory, connections).
    • Package Updates: Last release in 2017; may need forking for Laravel 10+ support.
  • Debugging:
    • Async issues harder to debug (e.g., dead-letter queues needed for failed events).
    • Log correlation IDs to trace events across sync/async boundaries.
  • Tooling:
    • Use Laravel Horizon or queue:failed-table for visibility into failed jobs.

Support

  • Troubleshooting:
    • Common issues:
      • Serialization errors (non-serializable events).
      • Broker connection drops (timeouts, auth failures).
      • Worker crashes (unhandled exceptions in listeners).
    • Debugging steps:
      1. Check failed_jobs table.
      2. Verify broker connectivity (queue:failed-table).
      3. Test with QUEUE_CONNECTION=sync for local debugging.
  • Documentation:
    • Limited official docs; rely on Symfony/Enqueue docs and GitHub issues.
    • Create internal runbooks for:
      • Worker restarts.
      • Broker failover.
      • Event retry policies.

Scaling

  • Horizontal Scaling:
    • Workers: Scale by adding more queue:work processes or containers.
    • Broker: Redis/RabbitMQ must handle increased message volume (e.g., Redis cluster for Redis).
  • **Performance
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
datacore/hub-sdk
alengo/sulu-http-cache-bundle
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
imbo/imbo-coding-standard
visualbuilder/filament-lottie
servicioslineaonce/starter-kit
atomcoder/laravel-reorderable
irajul/filament-shadcn-theme
agtp/agtp-php
agtp/mod-php
centraldesktop/protobuf-php
trappistes/laravel-custom-fields
splash/sonata-admin
splash/metadata