With the rise of microservices, it becomes crucial to connect applications properly and a Message Bus EIP helps here a lot.

It is a pattern that enables separate applications to work together, but in a decoupled fashion such that applications can be easily added or removed without affecting the others.

You may have CRM, PIM, Shop, Backend, Accounting, Warehouse, Payment and other services in your infrastructure talking to each other without being coupled.

I took Magento and Symfony applications for a demo, and I am going to connect them to a message bus. Magento will send a message whenever a product is updated. Symfony application subscribes to those messages. The consumer part could do product audit or export, update search index or send translate requests. There will be a plain PHP script that consumes messages as well, just to illustrate how simple it is to connect to MB.

I suppose you already have setup and running RabbitMQ server on localhost with default settings. If not, here’s a docker command to run a RabbitMQ container:

$ docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3-management

Magento

Let’s start from Magento. First, we have to install Magento Enqueue extension and secondly subscribe to the product after save event. To subscribe, we have to add some XML code as shown here:

In the subscriber, we have to take data_object (the product) and send it to the topic manento_product_updated .

The message comes to the message bus and is routed to every application which is interested in it. If a consumer is down, it does not affect producer in any way and messages are not lost. They stay in the queue till the consumer is back to normal work. We do not have to do anything with Magento if we remove a consumer application from our infrastructure.

Symfony

The post “Getting Started with RabbitMQ in Symfony” covers how to install and configure the EnqueueBundle. Let’s move on. In this post, Symfony application plays a consumer role. It has to subscribe to magento_product_updated topic and do something whenever a product is updated. You have to configure the bundle correctly(make sure you set unique client’s app_name ), create a processor class and register it as a service.

Finally, you have to run the consume command:

$ ./bin/console enqueue:consume -vvv --setup-broker

Plain php

The last part is here just to show you that you do not need Magento or Symfony or any other framework. It is deadly simple to use message bus from plain PHP. Let’s install Enqueue simple client and AMQP transport:

$ composer require enqueue/simple-client enqueue/amqp-ext

The next 30 lines of code are what you need to send and consume messages from\to the message bus:

and run:

php enqueue_consume.php

Conclusion

The message bus is a great pattern. It allows to build decoupled applications, schedule background jobs, scale the load, isolate the legacy or migrate from it. The Enqueue brings it to PHP world and makes it super easy to use.

Consider using message bus over REST API when you do not need synchronous calls.