How to implement RabbitMQ in Magento 2 CE

RabbitMQ is open source message broker. In this post I will demonstrate how install RabbitMQ client in Magento2 and how to create producer and consumer.

About Rabbit you can read more on url: https://www.rabbitmq.com/, also very good tutorial is on url: https://www.rabbitmq.com/tutorials/tutorial-one-python.html.

If you want to use RabbitMQ in Magento 2 then you have to install php client for Rabbit.

Client for PHP you can find on https://github.com/php-amqplib/php-amqplib.

If you want to implement RabbitMQ messaging in Magento then you should follow next steps.

For first step you should to execute next shell command in Magento root folder:

After that you can find php client on location: magento_root/vendor/php-amqplib.

Next steps are creating RabbitMQ producer and consumer. Producer is in charge for sending messages and consumer is worker which is in charge for processing messages. You can have more consumers.

Let’s start with class producer which will be in charge for sending messages to RabbitMQ.

And last thing is creating consumer class which will be in charge for processing messages:

I put consumer class on location: magento_root/workers/rabbitMQConsumer.php.  This is our worker. You can start worker with command from shell: php rabbitMQConsumer.php but in production environment you can run your workers with http://supervisord.org/index.html.

Below you can see RabbitMQ consumer in action. Consumer received four messages and display them.

In Magento you can use RabbitMQ for different jobs. I used RabbitMQ for image processing (when customer click to place order on checkout we had to process image with ImageMagic, very difficult operation), email sending and some action where crontab (cron) was not good or acceptable solution.

This is only small example how to setup basic environment for RabbitMQ and I hope that will be helpful 😉

You may also like...

Leave a Reply