Add message to queue

Overview

In this guide you'll learn how to create a message and add it to the queue.
Shopware integrates with the Symfony Messenger component and Enqueue. This gives you the possibility to send and handle asynchronous messages.
A message is a simple PHP object that you want to dispatch over the MessageQueue. It must be serializable and should contain all necessary information that your handlers need to process the message.
It will be wrapped in an envelope by the message bus that dispatches the message.

Prerequisites

As most guides, this guide is also built upon the Plugin base guide, but you don't necessarily need that. It will use an example service, so if you don't know how to add a custom service yet, have a look at our guide about Adding a custom service. Furthermore, registering classes or services to the DI container is also not explained here, but it's covered in our guide about Dependency injection, so having this open in another tab won't hurt.

Create a message

First, we have to create a new message class in the directory <plugin root>/MessageQueue/Message. In this example, we create a SmsNotification that contains a string with content.
Here's an example:
<plugin root>/src/MessageQueue/Message/SmsNotification.php
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\MessageQueue\Message;
4
5
class SmsNotification
6
{
7
private string $content;
8
9
public function __construct(string $content)
10
{
11
$this->content = $content;
12
}
13
14
public function getContent(): string
15
{
16
return $this->content;
17
}
18
}
Copied!

Send a message

After we've created our notification, we will create a service that will send our SmsNotification. We will name this service ExampleSender. In this service we need to inject the Symfony\Component\Messenger\MessageBusInterface, that is needed to send the message through the desired bus, which is called messenger.bus.shopware.
<plugin root>/src/Service/ExampleSender.php
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\Service;
4
5
use Swag\BasicExample\MessageQueue\Message\SmsNotification;
6
use Symfony\Component\Messenger\MessageBusInterface;
7
8
class ExampleSender
9
{
10
private MessageBusInterface $bus;
11
12
public function __construct(MessageBusInterface $bus)
13
{
14
$this->bus = $bus;
15
}
16
17
public function sendMessage(string $message): void
18
{
19
$this->bus->dispatch(new SmsNotification($message));
20
}
21
}
Copied!
If we want to add metadata to our message, we can dispatch an Symfony\Component\Messenger\Envelope in our service instead with the necessary stamps. In this example below, we use the Symfony\Component\Messenger\Stamp\DelayStamp, which tells the queue to process the message later.
<plugin root>/src/Service/ExampleSender.php
1
public function sendMessage(string $message): void
2
{
3
$message = new SmsNotification($message);
4
$this->bus->dispatch(
5
(new Envelope($message))
6
->with(new DelayStamp(5000))
7
);
8
}
Copied!

Encrypted messages

As the sent messages may travel through some 3rd party services you may want to encrypt messages containing sensible information. To send encrypted messages simply use the encrypted.messenger.bus.shopware rather than the messenger.bus.shopware message bus. The encrypted bus will handle encryption and decryption for you.
<plugin root>/src/Resources/config/services.xml
1
<service id="Swag\BasicExample\Service\ExampleSender">
2
<argument type="service" id="encrypted.messenger.bus.shopware"/>
3
</service>
Copied!

Next steps

Now that you know how to create a message and add it to the queue, let's create a handler to process our message. To do this, head over to our "Add message handler" guide: