letsgoi / laravel-domain-events-messaging
Package to publish/consume domain events messages from Laravel apps
Installs: 4 761
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 5
Forks: 0
Open Issues: 0
Requires
- php: ^8.3
- ext-json: *
- aws/aws-sdk-php: ^3.134
- illuminate/console: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- letsgoi/php-code-style: ^1.3
- orchestra/testbench: ^9.1
- phpunit/phpunit: ^11.2
This package is auto-updated.
Last update: 2024-11-10 11:22:10 UTC
README
This package allows to send/consume domain message events from your laravel app. This can be used to communicate between services or in the same application via queues.
It's divided in two main elements:
- Publisher: To publish messages on queue.
- Consumer: Read messages from queue and execute jobs/events.
Requirements
- PHP >= 8.3
- Laravel >= 10.0
Instalation
- Require package with composer:
composer require letsgoi/laravel-domain-events-messaging
- Publish configuration:
php artisan vendor:publish --provider="Letsgoi\DomainEventsMessaging\DomainEventsMessagingServiceProvider" --tag="config"
Service Provider will be automatically registered, however if you want to add it manually, you must add this to your config/app.php
file:
'providers' => [ // ... Letsgoi\DomainEventsMessaging\DomainEventsMessagingServiceProvider::class, ];
Usage
Publisher
Domain Events Messaging Publisher will publish messages to exchange defined on config file (on DOMAIN_EVENTS_PUBLISHER_CONNECTION
env variable).
Publishing messages
Just use publish
method on DomainEventsMessagingPublisher
facade to send messages with event type:
DomainEventsMessagingPublisher::publish('message.subject', 'message content');
Consumer
Domain Events Messaging Consumer will consume messages from the queue driver defined on config file (on DOMAIN_EVENTS_CONSUMER_CONNECTION
env variable) and launch jobs/events setted on config file (config/domain_events_messaging.php
) by message subject.
Defining events:
On config file you must set the messages to be consumed and the job/event to be dispatched when this message will be consumed:
'consumer' => [ // ... 'events' => [ 'message.subject' => Event::class, 'message.other' => Job::class, // ... ] ]
When consumer read some message with subject defined on this config, the job/event will be launch with the array $payload
setted on his constructor.
Jobs/Events
The class defined on config file to be dispatched on message, could be:
- Laravel Job: It must use the
Illuminate\Foundation\Bus\Dispatchable
trait. - Laravel Event: It must use the
Illuminate\Foundation\Events\Dispatchable
trait.
Both will be receive on his constructor message as string:
public function __construct(string $message)
You can queue the jobs as a normal job on Laravel.
Running consumer:
To run the consumer as a daemon you must execute:
php artisan domain-events:consume
This task should be monitored with some like supervisor to be running on fails.
Drivers
AWS SNS-SQS
To use AWS SNS or AWS SQS driver, you must follow this instructions:
- Add (and fill) this variables on your .env file:
# AWS IAM
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=
# SNS
AWS_DOMAIN_EVENTS_SNS_TOPIC_ARN=
# SQS
AWS_SQS_DOMAIN_EVENTS_URL=
Testing
Run tests:
composer test
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.