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

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.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Event-Driven Alignment: The package aligns well with Laravel’s built-in Event System (e.g., 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.
  • Pub/Sub Pattern: The library’s design mirrors Laravel’s event system but extends it with async drivers (RabbitMQ, File), making it suitable for microservices or high-throughput applications where synchronous event handling is a bottleneck.
  • Laravel Integration Points:
    • Can replace or supplement Laravel’s Event facade for async use cases.
    • Works alongside Laravel’s Queues (e.g., dispatch(new Job)) but with event-driven semantics.
    • Compatible with Laravel’s Service Container for dependency injection.

Integration Feasibility

  • Low Friction: The package is a drop-in replacement for Laravel’s EventDispatcher with minimal changes required (e.g., swapping Event::dispatch() for $asyncDispatcher->dispatch()).
  • Driver Flexibility: Supports RabbitMQ (production-grade) and File (development/testing), with extensibility for custom drivers (e.g., Redis, Kafka, or Laravel’s queue system).
  • Backward Compatibility: Events must implement AsyncEventInterface (similar to Laravel’s ShouldBeStringable or custom interfaces), requiring minimal refactoring if existing events are already async-friendly.

Technical Risk

  • Archived Status: The package is archived (no active maintenance), raising risks:
    • Security: No updates for vulnerabilities (e.g., RabbitMQ client deps).
    • Compatibility: May break with newer Laravel/PHP versions (e.g., PHP 8.2+ features).
    • Documentation: Limited examples or community support.
  • Design Trade-offs:
    • Global Driver Registration: Adding a driver without specifying events ($ed->addDriver($driver)) fires it for all events, which could lead to unintended side effects (e.g., performance overhead, duplicate processing).
    • No Built-in Retries/Dead Letter Queues: Unlike Laravel Queues, this package lacks native retry logic or DLQs for failed events.
  • Testing Gaps:
    • No dependents or benchmarks to validate performance under load.
    • File driver is not production-ready (race conditions, disk I/O).

Key Questions

  1. Why Async Events?
    • Are you replacing synchronous events, or adding async to an existing system?
    • What’s the expected throughput (e.g., 1000 events/sec)? RabbitMQ may be overkill for low-volume use.
  2. Driver Selection:
    • Is RabbitMQ available in your infrastructure? If not, will you build a custom driver (e.g., for Laravel Queues)?
  3. Error Handling:
    • How will failed events be handled? Will you wrap the dispatcher in a retry mechanism?
  4. Monitoring:
    • How will you track async event success/failure (e.g., logging, metrics)?
  5. Alternatives:
    • Could Laravel’s Queues or Horizon suffice? This package adds complexity without built-in queue management.
  6. Migration Path:
    • How will you phase out synchronous events? Will you use feature flags or parallel dispatch?

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Replaces: Illuminate\Events\Dispatcher for async use cases.
    • Complements: Laravel Queues (e.g., dispatch events to queues for async processing).
    • Extends: Laravel’s Service Providers (register the dispatcher as a singleton).
  • Tech Stack Requirements:
    • RabbitMQ Driver: Requires a RabbitMQ server (or alternative like CloudAMQP).
    • File Driver: Only for testing; not suitable for production.
    • PHP 7.4–8.1: Check compatibility with your Laravel version (e.g., Laravel 9+ may need adjustments).

Migration Path

  1. Phase 1: Pilot Events
    • Identify 2–3 non-critical events to migrate (e.g., UserRegistered, OrderProcessed).
    • Implement AsyncEventInterface for these events.
    • Register the AsyncEventDispatcher in AppServiceProvider:
      $this->app->singleton(AsyncEventDispatcher::class, function ($app) {
          $dispatcher = new AsyncEventDispatcher();
          $dispatcher->addDriver(new RabbitMQDriver(), 'event.name');
          return $dispatcher;
      });
      
    • Replace synchronous Event::dispatch() with $dispatcher->dispatch().
  2. Phase 2: Driver Validation
    • Test RabbitMQ driver under load (e.g., using Laravel Dusk or custom scripts).
    • Monitor for missed events or duplicates.
  3. Phase 3: Full Rollout
    • Gradually replace synchronous events with async equivalents.
    • Use feature flags to toggle between old/new dispatchers during transition.
  4. Phase 4: Custom Drivers (Optional)
    • If RabbitMQ is overkill, build a custom driver using Laravel Queues:
      class QueueDriver implements AsyncEventDriverInterface {
          public function handle(AsyncEventInterface $event) {
              dispatch(new ProcessEventJob($event));
          }
      }
      

Compatibility

  • Laravel Events:
    • Existing events won’t work without implementing AsyncEventInterface. Add:
      use BDK\AsyncEventDispatcher\AsyncEventInterface;
      
      class UserRegistered implements AsyncEventInterface {
          // ...
      }
      
    • Use traits or mixins to retroactively add the interface to existing events.
  • Middleware/Listeners:
    • Async events cannot use Laravel’s EventServiceProvider listeners (since they’re fire-and-forget). Design listeners to be idempotent.
  • Testing:
    • Mock the AsyncEventDispatcher in unit tests (e.g., using Mockery to verify events are dispatched to the driver).

Sequencing

  1. Infrastructure First:
    • Set up RabbitMQ (or alternative) before integrating the package.
  2. Event-by-Event:
    • Prioritize events with the highest async value (e.g., external API calls, analytics).
  3. Monitoring Early:
    • Add logging for dispatched events (e.g., dispatcher->dispatch($event) logs event name + timestamp).
  4. Fallback Plan:
    • Implement a hybrid dispatcher that falls back to synchronous dispatch if async fails (e.g., RabbitMQ unavailable).

Operational Impact

Maintenance

  • Vendor Risk:
    • No Updates: The archived status means you’ll need to maintain the package internally (e.g., fork and update dependencies).
    • Dependency Management: RabbitMQ client (e.g., php-amqplib) may require manual updates for security patches.
  • Customization:
    • Expect to extend the package (e.g., add retry logic, custom drivers, or monitoring).
    • Document internal changes for onboarding.

Support

  • Debugging Challenges:
    • Async events are harder to debug than synchronous ones (e.g., no immediate feedback on failure).
    • RabbitMQ-specific issues (e.g., connection drops) may require AMQP expertise.
  • Tooling Gaps:
    • No built-in Horizon-like dashboard for monitoring async events.
    • Consider integrating with Laravel Telescope or Prometheus for metrics.
  • Team Skills:
    • Requires familiarity with pub/sub systems (RabbitMQ) and async programming.

Scaling

  • Performance:
    • RabbitMQ Driver: Scales well for high throughput but adds latency (~10–100ms for network I/O).
    • File Driver: Not scalable (disk I/O bottleneck).
  • Resource Usage:
    • RabbitMQ consumer processes must be monitored for memory leaks (e.g., unacked messages).
    • Consider prefetch counts to control message load on consumers.
  • Horizontal Scaling:
    • Multiple Laravel instances can dispatch to the same RabbitMQ queue, but consumers must handle duplicates (idempotency required).

Failure Modes

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.

Ramp-Up

  • Onboarding Time:
    • Developers: 1–2 days to understand async event patterns and RabbitMQ basics.
    • Ops: 1 week to set up RabbitMQ, monitoring, and failover.
  • Training Needs:
    • Async programming concepts (e.g., eventual
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.
nasirkhan/laravel-sharekit
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony