nuwber / rabbitevents
The Nuwber RabbitEvents package
Fund package maintenance!
masterjus
Installs: 403 532
Dependents: 2
Suggesters: 0
Security: 0
Stars: 121
Watchers: 10
Forks: 36
Open Issues: 1
Requires
- php: ^8.1
- ext-json: *
- enqueue/amqp-lib: ^0.10.19
- illuminate/console: 9 - 11
- illuminate/container: 9 - 11
- illuminate/events: 9 - 11
- illuminate/support: 9 - 11
Requires (Dev)
- mockery/mockery: ^1.6.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5|^11.0
- vlucas/phpdotenv: ^v5.4.1
Suggests
- ext-amqp: Using this extension makes your app faster. If you're using it you need to install enqueue/amqp-ext.
- ext-pcntl: Required to use all features of the worker.
- enqueue/amqp-ext: This package is necessary if you want to use `ext-amqp`.
Replaces
- rabbitevents/foundation: v8.2.1
- rabbitevents/listener: v8.2.1
- rabbitevents/publisher: v8.2.1
- 8.x-dev
- v8.2.1
- v8.2.0
- v8.1.5
- v8.1.4
- v8.1.3
- v8.1.2
- v8.1.1
- v8.1.0
- v8.1.0-beta.1
- v8.0.1
- v8.0.0
- 7.x-dev
- v7.3.0
- v7.2.0
- v7.1.1
- v7.1.0
- v7.0.1
- v7.0
- v7.0-rc.1
- v7.0-beta.4
- v7.0-beta.3
- v7.0-beta.2
- v7.0-beta.1
- 6.x-dev
- v6.1.0
- v6.0.6
- v6.0.5
- v6.0.4
- v6.0.3
- v6.0.2
- v6.0.1
- v6.0
- v6.0.0-RC2
- v6.0.0-RC1
- v5.7
- v5.6
- v5.5
- v5.4
- v5.3
- v5.2
- v5.1
- v5.0
- v5.0-beta.1
- v4.3
- v4.2
- v4.1
- v4.0
- v4.0-beta.2
- 4.0-beta.1
- v3.1
- v3.0
- v3.0-beta2
- v3.0-beta1
- v2.0
- v1.4
- v1.3
- v1.2
- v1.1
- v1.0
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-master
This package is auto-updated.
Last update: 2024-12-10 09:28:49 UTC
README
Let's imagine a use case: a User made a payment. You need to handle this payment, register the user, send him emails, send analytics data to your analysis system, and so on. The modern infrastructure requires you to create microservices that do their specific job and only it: one handles payments, one is for user management, one is the mailing system, one is for analysis. How to let all of them know that a payment succeeded and handle this message? The answer is "To use RabbitEvents".
Once again, the RabbitEvents library helps you publish an event and handle it in another app. It doesn't make sense to use it in the same app because Laravel's Events work better for that.
Important
Attention version 7 users! A new version 7.3.0 has been released with ext-amqp support (similar to version 8.2). Update for improved performance.
Demo
Table of Contents
- Installation via Composer
- Upgrade from 7.x to 8.x
- Publisher component
- Listener component
- Examples
- Speeding up RabbitEvents
- Non-standard use
- License
Installation via Composer
You can use Composer to install RabbitEvents into your Laravel project:
composer require nuwber/rabbitevents
Configuration
After installing RabbitEvents, publish its config and a service provider using the rabbitevents:install
Artisan command:
php artisan rabbitevents:install
This command installs the config file at config/rabbitevents.php
and the Service Provider file at app/providers/RabbitEventsServiceProvider.php
.
The config file is very similar to the queue connection, but with the separate config, you'll never be confused if you have another connection to RabbitMQ.
<?php use Enqueue\AmqpTools\RabbitMqDlxDelayStrategy; return [ 'default' => env('RABBITEVENTS_CONNECTION', 'rabbitmq'), 'connections' => [ 'rabbitmq' => [ 'driver' => 'rabbitmq', 'exchange' => env('RABBITEVENTS_EXCHANGE', 'events'), 'host' => env('RABBITEVENTS_HOST', 'localhost'), 'port' => env('RABBITEVENTS_PORT', 5672), 'user' => env('RABBITEVENTS_USER', 'guest'), 'pass' => env('RABBITEVENTS_PASSWORD', 'guest'), 'vhost' => env('RABBITEVENTS_VHOST', 'events'), 'delay_strategy' => env('RABBITEVENTS_DELAY_STRATEGY', RabbitMqDlxDelayStrategy::class), 'ssl' => [ 'is_enabled' => env('RABBITEVENTS_SSL_ENABLED', false), 'verify_peer' => env('RABBITEVENTS_SSL_VERIFY_PEER', true), 'cafile' => env('RABBITEVENTS_SSL_CAFILE'), 'local_cert' => env('RABBITEVENTS_SSL_LOCAL_CERT'), 'local_key' => env('RABBITEVENTS_SSL_LOCAL_KEY'), 'passphrase' => env('RABBITEVENTS_SSL_PASSPHRASE', ''), ], 'read_timeout' => env('RABBITEVENTS_READ_TIMEOUT', 3.), 'write_timeout' => env('RABBITEVENTS_WRITE_TIMEOUT', 3.), 'connection_timeout' => env('RABBITEVENTS_CONNECTION_TIMEOUT', 3.), 'heartbeat' => env('RABBITEVENTS_HEARTBEAT', 0), 'persisted' => env('RABBITEVENTS_PERSISTED', false), 'lazy' => env('RABBITEVENTS_LAZY', true), 'qos' => [ 'global' => env('RABBITEVENTS_QOS_GLOBAL', false), 'prefetch_size' => env('RABBITEVENTS_QOS_PREFETCH_SIZE', 0), 'prefetch_count' => env('RABBITEVENTS_QOS_PREFETCH_COUNT', 1), ] ], ], 'logging' => [ 'enabled' => env('RABBITEVENTS_LOG_ENABLED', false), 'level' => env('RABBITEVENTS_LOG_LEVEL', 'info'), 'channel' => env('RABBITEVENTS_LOG_CHANNEL'), ], ];
Upgrade from 7.x to 8.x
PHP 8.1 required
RabbitEvents now requires PHP 8.1 or greater.
Supported Laravel versions
RabbitEvents now supports Laravel 9.0 or greater.
Removed --connection
option from the rabbitevents:listen
command
There's an issue #98 that still needs to be resolved. The default connection is always used instead.
RabbitEvents Publisher
The RabbitEvents Publisher component provides an API to publish events across the application structure. More information about how it works can be found on the RabbitEvents Publisher page.
RabbitEvents Listener
The RabbitEvents Listener component provides an API to handle events that were published across the application structure. More information about how it works can be found on the RabbitEvents Listener page.
Speeding up RabbitEvents
To enhance the performance of RabbitEvents, consider installing the php-amqp
extension along with the enqueue/amqp-ext
package.
By doing so, RabbitEvents will utilize the enqueue/amqp-ext
package instead of the default enqueue/amqp-lib
package.
This substitution is advantageous because the C-written php-amqp
package significantly outperforms the PHP-written enqueue/amqp-lib
package.
You can install the php-amqp
extension using the following command:
pecl install amqp
or use the way you prefer. More about it can be found here.
Next, install the enqueue/amqp-ext
package with the following command:
composer require enqueue/amqp-ext
No additional configuration is required.
Non-standard use
If you're using only one part of RabbitEvents, you should know a few things:
-
You remember, we're using RabbitMQ as the transport layer. In the RabbitMQ Documentation, you can find examples of how to publish your messages using a routing key. This routing key is the event name, like
something.happened
from the examples above. -
RabbitEvents expects that a message body is a JSON-encoded array. Every element of an array will be passed to a Listener as a separate variable. For example:
[ { "key": "value" }, "string", 123 ]
There are 3 elements in this array, so 3 variables will be passed to a Listener (an array, a string, and an integer). If an associative array is being passed, the Dispatcher wraps this array by itself.
License
RabbitEvents is open-sourced software licensed under the MIT license.