Installation

For installation please use docker as the following:

  1. docker pull rabbitmq:3-management
  2. After pulling and starting the image please forward the ports with this command docker run --rm -it -p 15672:15672 -p 5672:5672 rabbitmq:3-management
  3. Test the installation with going to this http://localhost:15672 username: guest password: guest

Congrats!!!

Configuration with Laravel

  1. Install rabbit-mq package to your project composer require vladimir-yuldashev/laravel-queue-rabbitmq
  2. Configure the env.php as the following
//change the queue connection type to
QUEUE_CONNECTION=rabbitmq

//Add the following 
RABBITMQ_HOST=127.0.0.1
RABBITMQ_PORT=5672
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_VHOST=/
  1. Add the following to the connections in the config/queue.php
'connections' => [
			  ...,
        'rabbitmq' => [

            'driver' => 'rabbitmq',
            'hosts' => [
                [
                    'host' => env('RABBITMQ_HOST', '127.0.0.1'),
                    'port' => env('RABBITMQ_PORT', 5672),
                    'user' => env('RABBITMQ_USER', 'guest'),
                    'password' => env('RABBITMQ_PASSWORD', 'guest'),
                    'vhost' => env('RABBITMQ_VHOST', '/'),
                    'queue' => env('RABBITMQ_QUEUE', 'default'),
                ]
            ],
        ],

    ],
  1. Create the job in both services with the same name php artisan make:job <job_name>
  2. In the publisher (dispatcher) service please leave the handle() function in the Job created empty and if any data is required to be passed you can define it in the __construct() function as the following:
class <job_name> implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $data;

    /**
     * Create a new job instance.
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {

    }
}
  1. Place the logic you need in the consumer (listener) service in the handle() function of the Job created, and also you can get the data in the __construct() as previously mentioned.
class <job_name> implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $data;
    /**
     * Create a new job instance.
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        Log::info('hi there from consumer');
        $order = Order::find($this->data['id']);
        $order->email_sent = true;
        $order->save();
    }
}
  1. To send the message from the publisher to the rabbitmq to be consumed, Please use the job dispatch static function as the following: OrderCreated::*dispatch*($order->toArray());