channext / channext-rabbitmq
The RabbitMQ integration for Channext services
Installs: 910
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:package
Requires
- illuminate/support: ^6|^7|^8|^9|^10|^11
- illuminate/validation: ^6|^7|^8|^9|^10|^11
- nyholm/psr7: ^1.8
- php-amqplib/php-amqplib: ^3.5
- ramsey/uuid: ^4.7
- sentry/sentry: ^4.0
- symfony/http-client: ^6.0
- symfony/process: ^5|^6|^7.0
This package is auto-updated.
Last update: 2024-04-22 07:53:28 UTC
README
A Laravel x RabbitMQ integration is a package to simplify the publishing and consuming of messages to and from a RabbitMQ server. This package expects you to use a topic exchange and bases its routing on the messages' routing keys.
Installation
- Run
composer require channext/channext-rabbitmq
in the root of your project - Run
php artisan vendor:publish
and choose the option "Provider: Channext\ChannextRabbitmq\Providers\RabbitMQServiceProvider" - If you want to use RabbitMQAuth facade and run
php artisan vendor:publish
then choose the option "Provider: Channext\ChannextRabbitmq\Providers\EventAuthServiceProvider" - Implement your logic inside EventAuthServiceProvider boot()
- If you want to use EventFail provider facade and run
php artisan vendor:publish
then choose the option "Provider: Channext\ChannextRabbitmq\Providers\EventFailServiceProvider" - Implement your logic inside EventFailServiceProvider boot()
- Add the correct RabbitMQ credentials to your .env file (see config/rabbitmq.php for the available variables. Also be sure to use different queue names for different services, otherwise only one service will act on the event other ones will miss it.)
- Run
php artisan config:cache
- Run
php artisan cache:clear
- Update the routes/topics.php file with the routes you want to listen to
- Run
php artisan rabbitmq:consume
to start consuming messages. Or you can usephp artisan rabbitmq:listen
to listen & consume every event in a separate app container. (This is more suitable for development purposes, but a bit slower.)
Examples
This package enables you to use a facade for executing simple publishing and consuming of RabbitMQ messages. To start using the facade simply import it in the class where you want to use it:
use Channext\ChannextRabbitmq\Facades\RabbitMQ;
To publish a message simply use the publish method in the RabbitMQ facade:
RabbitMQ::publish($body, $routingKey);
Where the body parameter is the data that you want to send and the routing key is the route that consumers can listen to. E.g. the moment a user is created the body should contain all the user data and the routing key could be "user.created".
To start consuming messages you should update the routes/topics.php file with all the routing keys you want your service to listen to. This is actually very similar to the way Laravel usually handles routing:
RabbitMQ::route('user.login', [Controller::class, 'test'], retry: false);
This route would only listen to messages with a routing key "user.login" and would execute the "test"-method in the controller.
The functions in the controllers called by the router should always accept the message as the first parameter:
public function test(AMQPMessage $message) { $messageBody = $message->all(); }