acdphp / laravel-queued-events
Dispatching Events into queues. This is especially useful for distributed systems events using fanout queues.
v2.1.0
2024-01-03 11:01 UTC
Requires
- php: ^8.0
Requires (Dev)
- ekino/phpstan-banned-code: ^1.0
- larastan/larastan: ^2.7
- laravel/pint: ^1.5
- orchestra/testbench: ^7.38
- pestphp/pest: ^1.23
- pestphp/pest-plugin-laravel: ^1.4
- phpstan/phpstan: ^1.10
README
Dispatching Events into queues. This is especially useful for distributed systems events using fanout queues.
Installation
- Install the package
composer require acdphp/laravel-queued-events
Usage
-
Extend
QueuedEvent
to your event.use Acdphp\QueuedEvents\Events\QueuedEvent; class UserCreatedEvent extends QueuedEvent { // Remove the Dispatchable trait public function __construct(public $object) { } }
-
Call
dispatch()
UserCreatedEvent::dispatch(['foo' => 'bar']);
- You may specify a queue connection and queue:
UserCreatedEvent::dispatch(['foo' => 'bar']) ->onConnection('your-fanout-queue-connection') ->onQueue('your-custom-queue');
- Utilities are also available:
dispatchIf()
,dispatchUnless()
// Dispatches if $condition is true UserCreatedEvent::dispatchIf($condition, ['foo' => 'bar']); // Dispatches if $condition is false UserCreatedEvent::dispatchUnless($condition, ['foo' => 'bar']);
-
Default dispatch methods are prefixed with
internal
UserCreatedEvent::internalDispatch(['foo' => 'bar']); UserCreatedEvent::internalDispatchIf(['foo' => 'bar']); UserCreatedEvent::internalDispatchUnless(['foo' => 'bar']);
Configuration
php artisan vendor:publish --tag=queued-events-config
QUEUED_EVENTS_QUEUE_CONNECTION
The default queue connection will be whatever your QUEUE_CONNECTION
is set. You may override this by setting QUEUED_EVENTS_QUEUE_CONNECTION
QUEUED_EVENTS_QUEUE
The default queue will be default
. You may override this by setting QUEUED_EVENTS_QUEUE
Notes
- Using Laravel helpers, like
event(...)
orapp('events')->dispatch(...)
, will dispatch the job internally. Only use::dispatch
,::dispatchIf
and::dispatchUnless
to dispatch on queue. - Use Laravel's queued listener if you're only using this in a monolithic application.
License
The MIT License (MIT). Please see License File for more information.