---
schema_version: "1"
id: e2c66a899fcee004811d001219253cf1b71312527ba3bcf0abe776d75f88c8ad
title: Publish Messages to an MQTT Broker with MQTT Connector
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/mqtt3-connector/1.0/mqtt3-connector-publish.md
canonical_url: https://docs.mulesoft.com/mqtt3-connector/1.0/mqtt3-connector-publish
source_site: docs.mulesoft.com
content_type: documentation
content_hash: c0dc250f7b3ba38670fa3495c1296353defc0e205223db89120bfea86420eca9
description: MuleSoft Documentation Site
release: "1.0"
latest: false
---
---
product: MQTT Connector
version: 1.0
---

# Publish Messages to an MQTT Broker with MQTT Connector

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

In the following example, you configure the Anypoint Connector for MQTT (MQTT Connector) **Publish**
operation, which enables you to publish a message to the desired topic and with a specific quality
of service (QoS).

1.  In Studio > **Mule Palette**, select **HTTP > Listener**.

2.  Drag **Listener** to the Studio canvas.

3.  Set **Path** to `/mqtt3/publish`.

4.  Click the plus sign (**+**) next to the **Connector configuration** field to configure a global
    element that can be used by all instances of the source in the app.

5.  Configure the HTTP Listener global element and click **OK**.

6.  In Studio > **Mule Palette**, select **MQTT3 > On New Message**.

7.  Drag **On New Message** to the Studio canvas.

8.  In the **On New Message** configuration screen, optionally change the value of the **Display
    Name** field.

9.  Click the plus sign (**+**) next to the **Connector configuration** field to configure a global
    element that can be used by all instances of the source in the app.

10. In **MQTT3 Config** > **Connection**, select **MQTT3 URL Connection**.

11. For **Client id generator**, select **Client id random suffix generator**.

12. Set the following fields:

    - **Client ID**: `smart-bentley-123`

    - **Username**: `usertest`

    - **Password**: `passtest`

    - **URL**: `tcp://127.0.0.1:1883`

13. Click **OK**.

14. In the **Publish** configuration screen, set the following fields:

    - **Topic**: `quotes/terryPratchett`

    - **Message** `#["Stories of imagination tend to upset those without one."]`

    - **QoS**: **AT\_LEAST\_ONCE (Default)**

    - **Is retained**: **True**

![MQTT Publish operation configuration in Studio](/mulesoft/current/en-us/mqtt3-connector/1.0/_images/mqtt3-publish.png)

In the **Configuration XML** editor, the `<mqtt3:publish>` and `topic` configurations look like
this:

```xml
<mqtt3:config name="MQTT_Config">
    <mqtt3:connection username="usertest" password="passtest" url="tcp://127.0.0.1:1883">
        <mqtt3:client-id-generator>
            <mqtt3:client-id-random-suffix-generator clientId="smart-bentley-123" />
        </mqtt3:client-id-generator>
    </mqtt3:connection>
</mqtt3:config>

<flow name="publishQuote">
    <http:listener config-ref="HTTP_Listener" path="/mqtt3/publish" />
    <mqtt3:publish config-ref="MQTT_Config" topic="quotes/terryPratchett" qos="AT_LEAST_ONCE" isRetained="true">
        <mqtt3:message>#["Stories of imagination sample quote."]</mqtt3:message>
    </mqtt3:publish>
</flow>
```

## Mark Messages as Retained

By default, a broker discards a received message on a topic for which there are no current
subscribers. If you want the broker to retain the message, configure **Is retained** to `True`. The
broker stores the last retained message and the corresponding quality of service for the selected
topic.  
Each client that subscribes to a topic pattern that matches the topic of the retained message
receives the retained message after they subscribe. The broker stores only one retained message per
topic. This enables new subscribers to receive the most current existing value rather than waiting
for the next update from a publisher.

## See Also

- [MQTT3 Connector Examples](https://docs.mulesoft.com/mqtt3-connector/1.0/mqtt3-connector-examples)

- [Salesforce Help](https://help.salesforce.com)
