How to Create Your own Messenger Transport

Once you have written your transport's sender and receiver, you can register your transport factory to be able to use it via a DSN in the Symfony application.

Create your Transport Factory

You need to give FrameworkBundle the opportunity to create your transport from a DSN. You will need a transport factory:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;

class YourTransportFactory implements TransportFactoryInterface
{
    public function createTransport(string $dsn, array $options): TransportInterface
    {
        return new YourTransport(/* ... */);
    }

    public function supports(string $dsn, array $options): bool
    {
        return 0 === strpos($dsn, 'my-transport://');
    }
}

The transport object needs to implement the TransportInterface (which combines the SenderInterface and ReceiverInterface):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use Symfony\Component\Messenger\Envelope;

class YourTransport implements TransportInterface
{
    public function get(): iterable
    {
        // ...
    }

    public function ack(Envelope $envelope): void
    {
        // ...
    }

    public function reject(Envelope $envelope): void
    {
        // ...
    }

    public function send(Envelope $envelope): Envelope
    {
        // ...
    }
}

Register your Factory

  • YAML
    1
    2
    3
    4
    # config/services.yaml
    services:
        Your\Transport\YourTransportFactory:
            tags: [messenger.transport_factory]
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    <!-- config/services.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/dic/services
            https://symfony.com/schema/dic/services/services-1.0.xsd">
    
        <services>
            <service id="Your\Transport\YourTransportFactory">
               <tag name="messenger.transport_factory"/>
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    5
    // config/services.php
    use Your\Transport\YourTransportFactory;
    
    $container->register(YourTransportFactory::class)
        ->setTags(['messenger.transport_factory']);
    

Use your Transport

Within the framework.messenger.transports.* configuration, create your named transport using your own DSN:

  • YAML
    1
    2
    3
    4
    5
    # config/packages/messenger.yaml
    framework:
        messenger:
            transports:
                yours: 'my-transport://...'
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    <!-- config/packages/messenger.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:framework="http://symfony.com/schema/dic/symfony"
        xsi:schemaLocation="http://symfony.com/schema/dic/services
            https://symfony.com/schema/dic/services/services-1.0.xsd
            http://symfony.com/schema/dic/symfony
            https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    
        <framework:config>
            <framework:messenger>
                <framework:transport name="yours" dsn="my-transport://..."/>
            </framework:messenger>
        </framework:config>
    </container>
    
  • PHP
    1
    2
    3
    4
    5
    6
    7
    8
    // config/packages/messenger.php
    $container->loadFromExtension('framework', [
        'messenger' => [
            'transports' => [
                'yours' => 'my-transport://...',
            ],
        ],
    ]);
    

In addition of being able to route your messages to the yours sender, this will give you access to the following services:

  1. messenger.sender.yours: the sender;
  2. messenger.receiver.yours: the receiver.