Download the PHP package girni/laravel-rabbitmq without Composer
On this page you can find all versions of the php package girni/laravel-rabbitmq. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download girni/laravel-rabbitmq
More information about girni/laravel-rabbitmq
Files in girni/laravel-rabbitmq
Package laravel-rabbitmq
Short Description Laravel RabbitMQ package that extends https://github.com/vyuldashev/laravel-queue-rabbitmq to support microservice communication.
License MIT
Informations about the package laravel-rabbitmq
Laravel RabbitMQ package that extends https://github.com/vyuldashev/laravel-queue-rabbitmq to support microservice communication.
Installation
In your composer.json
file add these lines of codes under require
section and repositories
:
After that changes simply run composer install
or composer update girni/laravel-rabbitmq
to make it installed.
The package will automatically register itself.
Add connection to config/queue.php
:
Publish laravel-rabbitmq.php
config file:
Usage
Define a queue in .env file
To ensure that our communication between applications runs smoothly, it is necessary to define separate queues for each application.
We can do it in .env
file.
Defining a RABBIT_QUEUE
will result that our application will consume a messages only from that queue. By making different queues for each application we have guarantee our messages won't be processed in incorrect way by other application.
Creating producer
Producing means nothing more than sending. A class that sends messages is a producer. Our producer classess should be as
simple as it's only possible. It should contains a message data passed by it's constructor and the ::name()
that must
be implemented due to implemented contract.
::name()
value is very important, it's being used to recognize which consumer we should run to handle the produced
message.
Dispatching message
To dispatch a message to a RabbitMQ we can use laravel dispatcher in several ways. More you can read here https://laravel.com/docs/9.x/queues#dispatching-jobs
It's important to define a queue on which we want to send a message by using ::onQueue()
method.
As it's mentioned before, each application should consume the messages ONLY from its own queue. So if we want our message to be processed by another application we need to
send it to this application's queue.
Message structure for non Laravel sender
JSON example:
Creating a consumer
Consumer class is nothing more than a "Handler" for a producer. This class is obliged to process the data that was sent by producer.
Register consumer
In your application in config\laravel-rabbitmq.php
file in consumers
array you have to register each consumer you want to use in your application.
Your config should look like:
Consuming messages
To start consuming produced messages we need to run our queue:
or standard laravel procedure
Advanced usage - Custom Message class
As a standard message store we implemented Girni\LaravelRabbitMQ\Message\BaseMessage::class
that has simple interface which operates on array keys.
It contains helper methods such as ::get($key), ::set($key)
to help client get/set data from/to a message.
However, in some cases our message can be very complex, and we would prefer to store its data in dedicated class (DTO) with object-oriented interface.
We can do it by adding Girni\LaravelRabbitMQConsumer\HasCustomMessage
interface to our consumer class, that comes with a method ::message()
.
In the body of this method you should pass a FQCN to your class.
IMPORTANT: Make sure your DTO class implements Girni\LaravelRabbitMQ\Message\MessageInterface
. The whole logic in which our library will create an object is hiding in ::fromArray(array $data)
method.
Testing
Changelog
Please see CHANGELOG for more information what has changed recently.
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-rabbitmq with dependencies
illuminate/support Version ^9.0|^10.0
vladimir-yuldashev/laravel-queue-rabbitmq Version ^13.0