Magento 2 6/18/2024

Event-driven Architecture with Mage-OS or Magento 2

Mage-OS provides an extensive module called Mage-OS Asynchronous Events which allows sending events to third-party systems like ERPs or middlewares. This can be incredibly useful for integrations, sending data at the moment when something happens in Magento / Mage-OS. A good example is the creation of an order — you might want to send the order data to a middleware, an ERP and/or a CRM.

While Adobe Commerce comes with its own solution for Event driven Architecture called Adobe I/O, Mage-OS has its official Asynchronous Events module which can also be used on Magento 2 OpenSource.

By default, data is sent via HTTP to predefined endpoints. Other subscription types are possible too — for example, there is an Amazon EventBridge integration too.

We recommend installing it with the two accompanying modules:

Install the modules with these commands:

composer require mage-os/mageos-async-events
composer require mage-os/mageos-common-async-events
composer require mage-os/mageos-async-events-admin-ui
bin/magento setup:upgrade

Please note that you will need a running Message Queue in your system - either RabbitMQ or the MySQL message queue. This is what makes this technology asynchronous.

Example: sending data to a low-code tool like n8n

While there are other low-code tools like Node-RED, we are using n8n internally so this is what we will be using as our example. For demonstration purposes, the data is then sent to Slack, posting a new message in a predefined channel.

Create a webhook in n8n

We can create an HTTP endpoint with a basic trigger node:

Create a webhook with n8n

It gives us a URL which we will need later when setting up our subscription. In order to observe the data, we set up an output node — connecting n8n with Slack, sending a message every time the event is executed:

The full workflow in n8n

Set up subscription

In our Magento / Mage-OS instance, we can now go to Stores -> Asynchronous Events -> Subscriptions:

Stores -> Asynchronous Events -> Subscriptions

There, we can add a new subscription. We select the event we want to observe (the changing of customer addresses in this case), and the target URL:

Create new subscription via admin form

If we now change a customer address in the Magento Backend, we see the results in Slack after a few seconds:

Slack message, sent by our n8n integration

If this doesn't work, you might have to restart your Message Queue Consumer. This is a long-running process. If it was started before you configured your new event, it might ignore it because it is working on an outdated configuration.

How it works

Sending an event is a two-step process.

1. Publish ID of changed dataset to the Message Queue

Usually, we use standard Magento events to do this lightweight step. The code can look like this:

Code of ID of changed dataset to the Message Queue

This code adds a simple message to the message queue, containing only the type of the event (customer.address.updated) and the ID of the affected dataset.

You can see https://github.com/mage-os/mageos-common-async-events/blob/main/Observer/CustomerAddressSaveAfterObserver.php for a full example.

2. Retrieve data from database and send it

This second step is executed by the message queue consumer which belongs to the AsyncEvents module. This is a long-running process, listening for new messages in the message queue and reacting to them.

As the message only contains the event type and an identifier, the consumer needs to know where to get the full data from. Usually, a PHP service class is used for that, defined in a file called etc/async_events.xml. For our example, it looks like this:

Screenshot of the code of async events

It can be your own PHP class, or an existing Magento repository, providing a method which accepts an identifier and returns a data object. Everything else is done by the AsyncEvents module: this data object, transformed to JSON, is sent to the configured target, i.e. the HTTP endpoint at n8n, where it can be processed.

Features of the AsyncEvents module

The module was created by Gowri Sankar, DevOps Engineer at Aligent, an agency based in Australia. The module was first published under the Aligent namespace, and integrated into Mage-OS in 2023. The accompanying modules, "Common Async Events" and "Async Events Admin UI", have been developed by Andreas von Studnitz (integer_net).

Trace events

All events are logged in Magento, including data and delivery success / failures:

Log of a single event

Retries and Replays

All transmissions can automatically be retried several times if they fail. The frequency and maximum number of retries can be configured in Magento. All events can also be executed again manually.

Scalability

Due to its asynchronous and decoupled architecture, using Event Driven Architecture with Magento / Mage-OS and the AsyncEvents module is very scalable, capable of transmitting several events per second. You can also set up your Message Queue listeners on a separate server in order to not affect the performance of your Magento server(s).

OpenSource

The modules are fully open source, using the MIT license. They can be used without charge.

Compatibility

The modules can be used in recent versions of Magento OpenSource, Mage-OS and Adobe Commerce. The 3.x branch runs on all versions since 2.4.4, while the 2.x is still supported and runs on Magento since 2.3.0.