Messaging Gateway

Message Gateway PHP

The Messaging Gateway encapsulates messaging-specific code (The code required to send or receive a Message) and separates it from the rest of the application code.

It takes Application specific data and convert it Message which then is sent via Message channel. This hide Messaging specific code, from user's code.

Command/Query/Event Buses are implementations of Messaging Gateways.

Ecotone aims for eliminating Framework related code from Business related code, that's why Gateway can defined as interface in user's code base. Ecotone is responsible for generating implementation for any interface.

Implementing custom Gateway

To implement custom Gateway, we will be using clear interfaces and message channel.

namespace Product;

use Ecotone\Messaging\Annotation\Gateway;

interface ProductGateway
    #[MessageGateway("buyProduct")]  // 2
    public function buy(string $product) : void;

// An Command Handler that we want to call from our Gateway
class ProductService
   public function buyProduct(string $product): void
      // do something
  1. By default gateway will be available under interface name in DI, in that case Product\ProductGateway. If you want to register it under different name for example "productGateway", then pass it to annotation #[ClassReference("productGateway")]

If you are using Symfony Integration, then it will be auto registered in Dependency Container with possibility to auto-wire the gateway.

As Command/Query/Event buses are Gateways, you can auto-wire them. They can be injected into Controller and called directly.

2. Gateway enables method to be used as Messaging Gateway. You may have multiple Gateways defined within interface. The "buyProduct" is a channel name that we will be requesting.

Building your own Bus

Using combination of Messaging Gateway and Router we can build our own Buses. Messaging Gateway will be responsible for building Message and sending to the Router. And Router will be then based on need route it by payload / headers or whatever is needed.

Invoking Handler directly

In some cases you may want to invoke Query/Command Handler directly, not via Bus. In that case you may define Message Gateway that routes to given Handler.

Service Command Handler:

class OrderService {
    public function makeOrder(string $order)
        // make order


interface OrderGateway {
    public function placeOrder(string $order): void;

This also works for Query Handlers. This way we can build an interface, which provides API for given set of behaviours e.g. OrderApi.

Invoking Aggregate's Handler directly

You may expose Aggregate's Command and Query Handlers through interface.


class Order {
    public function makeOrder(CancelOrder $command)
        // do something


interface OrderGateway {
    public function placeOrder(#[AggregateIdentifier] string $orderId, #[Payload] CancelOrder $command)): void;

Gateway reply

    public function getPrice(string $productName) : int;

Gateway may return values, but as you probably remember, everything is connected via Message Channels. So how does we get the reply? During Message building, gateway adds header replyChannel which contains automatically created Channel. During Endpoint's method invocation, if any value was returned it will be sent via reply Channel. This way gateway may receive the reply and return it.

Gateway reply Content Type

If you have registered Converter for specific Media Type, then you can tell Ecotone to convert result of any Gateway to specific format. This is especially useful, when we are dealing with QueryBus, when we want to return the result to the caller of the request. In order to do this, we need to make use of Metadataand replyContentType header.

   public function __construct(private QueryBus $queryBus)
       $this->queryBus = $queryBus;   
   public function getTicketStatusAction(Request $request) : Response
      return new Response(
            new GetTicketStatusQuery($request->get("ticketId")),
            ["replyContentType" => "application/json"]

Repository Interfaces

Repository implemented via interfaces are just higher level abstraction of Message Gateway. You enable them by marking attribute #[Repository].

interface OrderRepository
    public function getOrder(string $twitId): Order;

    public function findOrder(string $twitId): ?Order;

    public function save(Twitter $twitter): void;

Read more about Repositories in Command Handling section.

Parameters to Message Conversion

In order to build Message, Parameter Converters are introduced. You may configure them manually or let Ecotone make use of default parameter converters.

public function placeOrder(#[Payload] Order $order, #[Header("executorId")] string $executorId);

Parameter converter types:

  • This will convert string passed under $content parameter to message payload

public function sendMail(#[Payload] string $content) : void;
  • This convert $content to message's payload and will add to headers under "receiverEmail" key value of $toEmail

public function sendMail(#[Payload] string $content, #[Header()] string $toEmail) : void;

Last updated