> For the complete documentation index, see [llms.txt](https://docs.ecotone.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ecotone.tech/modelling/microservices-php/message-publisher.md).

# Message Publisher

Message Publisher for publishing to external message brokers

You're integrating with a third-party service that listens on RabbitMQ — they don't run Ecotone, they have their own wire format. You don't want your business code to know about the AMQP client, the topic name, or the serialization format. **Message Publisher** is a typed interface that publishes raw messages to an external broker; if you swap brokers later, the call sites don't change.

### Modules Providing Support

* [RabbitMQ Module](/modules/amqp-support-rabbitmq.md#message-publisher)

## Message Publisher Interface

```php
interface MessagePublisher
{

    // 1
    public function send(string $data, string $sourceMediaType = MediaType::TEXT_PLAIN) : void;

    // 2
    public function sendWithMetadata(string $data, array $metadata, string $sourceMediaType = MediaType::TEXT_PLAIN) : void;

    // 3
    public function convertAndSend(object|array $data) : void;

    // 4
    public function convertAndSendWithMetadata(object|array $data, array $metadata) : void;
}
```

1. `send` - Send a `string type` via Publisher. It does not need any conversion, you may add additional `Media Type` of `$data`.
2. `sendWithMetadata` - Does the same as `send,` allows for sending additional [Meta data](/tutorial-php-ddd-cqrs-event-sourcing/php-metadata-method-invocation.md#metadata).
3. `convertAndSend` - Allow for sending types, which needs conversion. Allow for sending objects and array, `Ecotone` make use of [Conversion system](/messaging/conversion/conversion.md) to convert `$data`.
4. `convertAndSendWithMetadata` - Does the same as `convertAndSend,` allow for sending additional [Meta data](/tutorial-php-ddd-cqrs-event-sourcing/php-metadata-method-invocation.md#metadata).

## How to use Message Publisher

Publisher is a special type of [Gateway](/messaging/messaging-concepts/messaging-gateway.md), which implements [Publisher interface](/modules/amqp-support-rabbitmq.md#available-actions).\
It will be available in your Dependency Container under passed `Reference name.`\
In case interface name `MessagePublisher:class` is used, it will be available using auto-wire.

```php
#[EventHandler] 
public function whenOrderWasPlaced(OrderWasPlaced $event, MessagePublisher $publisher) : void
{
    $publisher->convertAndSendWithMetadata(
        $event,
        [
            "system.executor" => "Johny"
        ]
    );
}
```

## High Throughput Publishing (Enterprise)

When the Publisher is configured with `withHighThroughputPublishing()`, messages published within the same scope are batched into single Broker writes:

```php
#[ServiceContext]
public function messagePublisher()
{
    return AmqpMessagePublisherConfiguration::create()
                ->withDefaultRoutingKey("orders")
                ->withHighThroughputPublishing();
}
```

On Brokers that can confirm a delivery after the fact - RabbitMQ, Kafka and SQS - the Publisher additionally exposes `publishDeferred`, which fires the message without blocking on Broker confirmation and returns a `Future`:

```php
$future = $publisher->publishDeferred($orderData);

$future->resolve(); // awaits delivery confirmation
```

DBAL and Redis Publishers batch, yet cannot defer confirmation, as their write blocks until the store confirms it. Publish batches there with `convertAndSend`.

Combined with `BatchMessage`, this allows publishing thousands of messages in single batched Broker operations. Read more in [Non-blocking Batched Delivery](/modelling/asynchronous-handling/non-blocking-batched-delivery.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.ecotone.tech/modelling/microservices-php/message-publisher.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
