> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreflux.org/llms.txt
> Use this file to discover all available pages before exploring further.

# MQTT Bridge Connector

> Connect two MQTT brokers for edge-to-cloud synchronization, multi-site connectivity, and backup systems

## MQTT Bridge Overview

The `MQTT_BRIDGE` connector connects your Coreflux broker to another MQTT broker and keeps selected topics in sync between them. Use it for edge-to-cloud synchronization, linking multiple sites, or mirroring data to a backup broker.

> [!TIP]
>
> **Like call forwarding for messages.** You decide which topics get forwarded and in which direction, and the bridge relays every matching message between the two brokers automatically.

### When to use it

* Push local sensor data from an edge device up to a cloud broker.
* Pull commands or configuration from a central broker down to remote sites.
* Keep two brokers continuously in sync for redundancy.

***

Let's look at how to define a bridge, starting with the most common scenarios.

## Quick Start

> [tab] Edge to cloud (out)
>
> Forward local sensor data up to a cloud broker over TLS:
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR EdgeToCloud WITH TYPE MQTT_BRIDGE
>     ADD SOURCE_CONFIG
>         WITH BROKER SELF
>     ADD DESTINATION_CONFIG
>         WITH BROKER_ADDRESS "iot.cloud-provider.com"
>         WITH BROKER_PORT '8883'
>         WITH CLIENT_ID "EdgeDevice001"
>         WITH USERNAME "edge_user"
>         WITH PASSWORD "secure_password"
>         WITH USE_TLS "true"
>     ADD MAPPING sensorData
>         WITH SOURCE_TOPIC "sensors/+/data"
>         WITH DESTINATION_TOPIC "edge-001/sensors/+/data"
>         WITH DIRECTION "out"
> ```

> [tab] Cloud to edge (in)
>
> Pull commands from a cloud broker down to the local broker:
>
> ```lot wrap  theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR CloudCommands WITH TYPE MQTT_BRIDGE
>     ADD SOURCE_CONFIG
>         WITH BROKER SELF
>     ADD DESTINATION_CONFIG
>         WITH BROKER_ADDRESS "iot.cloud-provider.com"
>         WITH BROKER_PORT '8883'
>         WITH CLIENT_ID "EdgeDevice001-Commands"
>         WITH USE_TLS "true"
>     ADD MAPPING commands
>         WITH SOURCE_TOPIC "local/commands/#"
>         WITH DESTINATION_TOPIC "edge-001/commands/#"
>         WITH DIRECTION "in"
> ```

> [tab] Bidirectional (both)
>
> Keep two brokers fully in sync in both directions:
>
> ```lot wrap  theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR FullSync WITH TYPE MQTT_BRIDGE
>     ADD SOURCE_CONFIG
>         WITH BROKER SELF
>     ADD DESTINATION_CONFIG
>         WITH BROKER_ADDRESS "partner-broker.example.com"
>         WITH BROKER_PORT '1883'
>         WITH CLIENT_ID "SyncClient"
>     ADD MAPPING bidirectionalSync
>         WITH SOURCE_TOPIC "shared/data/#"
>         WITH DESTINATION_TOPIC "partner/data/#"
>         WITH DIRECTION "both"
> ```

***

## Connection Configuration

A bridge has two sides, `ADD SOURCE_CONFIG` and `ADD DESTINATION_CONFIG`. Each side is either this broker (`WITH BROKER SELF`) or a remote broker set with `BROKER_ADDRESS`. Either side can be `SELF`. On a `SELF` side, the block's other settings are ignored.

### Source, destination and direction

Each remote side gets its own MQTT client, which subscribes to that side's topic. A `SELF` side has no client: the bridge reads the topic from this broker.

| Side | Subscribes to | For mappings with `DIRECTION` |
| - | - | - |
| `SOURCE_CONFIG` | `SOURCE_TOPIC` | `out` or `both` |
| `DESTINATION_CONFIG` | `DESTINATION_TOPIC` | `in` or `both` |

* `out` forwards source → destination: it reads `SOURCE_TOPIC` and publishes to `DESTINATION_TOPIC`.
* `in` forwards destination → source: it reads `DESTINATION_TOPIC` and publishes to `SOURCE_TOPIC`.
* `both` does both. It is the default when a mapping leaves `DIRECTION` out.

### Login is optional

Leave `USERNAME` and `PASSWORD` out for a broker that needs no login. That side then connects without credentials. `CLIENT_CERT_PASS` is needed only when the client key is encrypted.

`-addConnector` refuses a bridge, with a message that names the setting, when a side:

* is not `WITH BROKER SELF` and has no `BROKER_ADDRESS`, or
* sets `PASSWORD` without `USERNAME`.

A `GET SECRET` or `GET ENV` value counts as set when you add the connector. It is resolved when the connector starts.

> [accordion] Broker Settings
>
> **BROKER** `string`
>
> Set to `SELF` (`WITH BROKER SELF`) to use this broker for this side. The block's other settings are then ignored.
>
> **BROKER\_ADDRESS** `string`
>
> IP address or hostname of the remote broker. Required unless the side is `WITH BROKER SELF`.
>
> **BROKER\_PORT** `integer`
>
> MQTT port of the remote broker. Default: 1883. TLS brokers usually listen on 8883.
>
> **CLIENT\_ID** `string`
>
> Client identifier for the bridge connection. Optional. When unset, the broker generates one that is unique per host, connector and endpoint (`cf-<host>-<connector>-src` or `cf-<host>-<connector>-dst`). Set it when the remote broker expects a specific client ID, and keep it distinct for each connector.

> [accordion] Authentication
>
> **USERNAME** `string`
>
> Login user name. Leave it out for a broker that needs no login.
>
> **PASSWORD** `string`
>
> Login password. Needs `USERNAME`. Use `GET SECRET` to keep it out of the connector definition.

> [accordion] TLS/Security
>
> **USE\_TLS** `boolean`
>
> Enable TLS encryption. Default: false.
>
> **ALLOW\_UNTRUSTED\_CERTS** `boolean`
>
> Allow untrusted certificates. Default: false.
>
> **SERVER\_CA\_CERT\_PATH** `string`
>
> Path to CA certificate file.
>
> **CLIENT\_CERT\_PATH** `string`
>
> Path to client certificate for mTLS, or a combined PEM with certificate and key.
>
> **CLIENT\_KEY\_PATH** `string`
>
> Path to a separate private key file. Leave it out when `CLIENT_CERT_PATH` is a combined PEM.
>
> **CLIENT\_CERT\_PASS** `string`
>
> Password of an encrypted client key. Leave it out when the key is not encrypted.

> [accordion] Connection Handling
>
> **RECONNECTION\_RETRIES** `integer`
>
> Number of reconnection attempts. Default: 5.

### When the remote broker is slow to answer

Adding a bridge waits at most **5 seconds** for a remote broker to accept the connection before the command replies. A remote broker that answers in time is connected when the reply arrives. One that is slow or silent keeps connecting in the background, and a failure is logged when it happens. Other broker commands are not held up meanwhile.

***

## Mapping Configuration

Each `ADD MAPPING` block defines which topics are bridged and in which direction.

> [accordion] Topic Mapping
>
> **SOURCE\_TOPIC** `string` *required*
>
> Topic on the source side. Read for `out`, written for `in`. Supports `+` and `#` wildcards.
>
> **DESTINATION\_TOPIC** `string` *required*
>
> Topic on the destination side. Written for `out`, read for `in`. Supports `+` and `#` wildcards.
>
> **DIRECTION** `string`
>
> Data flow direction: `out` (source → destination), `in` (destination → source), or `both`. Default: `both`.

***

## QoS and RETAIN

The bridge forwards each message, in either direction, with the **QoS and RETAIN flag it was published with**. A retained message becomes retained on the other broker, and clearing it there (an empty retained publish) clears the bridged copy too. QoS 1 and QoS 2 messages keep their delivery guarantee on the forwarded hop. A subscriber still receives at most the QoS of its own subscription, as MQTT specifies.

There is no mapping setting to change QoS or RETAIN; the publisher's values always apply.

> [!NOTE]
>
> **The receiving broker's limits win.** If a remote broker accepts at most QoS 1, or does not support retained messages, the bridge publishes at that maximum QoS and without RETAIN instead of being disconnected. Some cloud MQTT services allow at most QoS 1.

> [!WARNING]
>
> **Retained messages replay on every reconnect.** Each time the bridge connects, the remote broker sends it every retained message under the mapped topics, and the bridge forwards them as retained. A change made on the local broker while the link was down is not forwarded, so for `in` and `both` mappings the remote's older retained value can replace the newer local one. A `#` mapping replays the whole retained tree. Keep each retained topic owned by one side, or republish it after the link recovers.

Each bridge connection keeps up to 64 QoS 1/2 publishes waiting for an acknowledgement at once. When a slow remote broker fills all 64, other connectors' queued updates wait too, and each connector's [overload policy](https://docs.coreflux.org/latest/lot-language/routes/store-and-forward.md) decides whether its producers wait or drop.

***

## Secure mTLS Bridge

For production links between sites, encrypt both endpoints and authenticate with mutual TLS:

```lot wrap  theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
DEFINE CONNECTOR SecureBridge WITH TYPE MQTT_BRIDGE
    ADD SOURCE_CONFIG
        WITH BROKER_ADDRESS "192.168.1.10"
        WITH BROKER_PORT '8883'
        WITH CLIENT_ID "SecureSource"
        WITH USE_TLS "true"
        WITH SERVER_CA_CERT_PATH "/certs/ca.pem"
        WITH CLIENT_CERT_PATH "/certs/client.pem"
        WITH CLIENT_CERT_PASS "cert_password"
    ADD DESTINATION_CONFIG
        WITH BROKER_ADDRESS "secure.cloud.com"
        WITH BROKER_PORT '8883'
        WITH CLIENT_ID "SecureDest"
        WITH USE_TLS "true"
        WITH ALLOW_UNTRUSTED_CERTS "false"
    ADD MAPPING secureMapping
        WITH SOURCE_TOPIC "secure/+/data"
        WITH DESTINATION_TOPIC "cloud/+/data"
        WITH DIRECTION "out"
```

***

## Best Practices

> [accordion] Use TLS for External Connections
>
> Always enable TLS when connecting to external brokers:
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> WITH USE_TLS "true"
> ```

> [accordion] Unique Client IDs
>
> If you set CLIENT\_ID, keep it unique per bridge connection to avoid conflicts. Two connections with the same client ID on one remote broker disconnect each other. Leave it unset to use the generated default.
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> WITH CLIENT_ID "EdgeDevice001-SensorBridge"
> ```

> [accordion] Test with Local Broker First
>
> Use `WITH BROKER SELF` during development:
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> ADD SOURCE_CONFIG
>     WITH BROKER SELF
> ```

> [accordion] Secure Credentials
>
> Store passwords securely and use app-specific passwords where available.

> [accordion] Handle Reconnection
>
> Configure appropriate retry settings:
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> WITH RECONNECTION_RETRIES '10'
> ```

***

## Troubleshooting

> [accordion] Bridge Connection Timeout
>
> * Verify IP address and port are correct
> * Check firewall allows outbound connections on the configured port
> * Ensure remote broker is running and accessible
> * Try increasing `RECONNECTION_RETRIES`
> * The add command replies after at most 5 seconds even if the remote broker has not answered. Check the broker log for the connection result. See [When the remote broker is slow to answer](#when-the-remote-broker-is-slow-to-answer)

> [accordion] Connector Refused When Added
>
> * The reply names the side and the setting
> * Give each remote side a `BROKER_ADDRESS`, or use `WITH BROKER SELF`
> * Set `USERNAME` with `PASSWORD`, or leave both out for a broker that needs no login

> [accordion] Not Authorized
>
> * Check `USERNAME` and `PASSWORD` for that side
> * A `GET SECRET` with no value on this broker is sent as no password. Check the secret exists with `-listSecrets`

> [accordion] TLS/Certificate Errors
>
> * Verify certificate paths are correct
> * Ensure certificates are valid and not expired
> * Check certificate chain is complete
> * For testing, use `ALLOW_UNTRUSTED_CERTS "true"` (not recommended for production)

> [accordion] Messages Not Bridging
>
> * Verify SOURCE\_TOPIC and DESTINATION\_TOPIC are correct
> * Check DIRECTION setting matches your expected data flow
> * If you set CLIENT\_ID, ensure it is unique across all connections
> * Check broker logs for subscription errors

> [accordion] Old Retained Value After a Reconnect
>
> * The remote broker replays its retained messages each time the bridge connects. See [QoS and RETAIN](#qos-and-retain)
> * Keep each retained topic owned by one broker, or republish the current value after the link recovers

***

## Next Steps

> [card] [System Connectors](https://docs.coreflux.org/latest/connectors/system/overview)
>
> Compare bridging and Sparkplug roles.

> [card] [Sparkplug B](https://docs.coreflux.org/latest/connectors/system/sparkplug-b.md)
>
> Publish and consume structured industrial MQTT data.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.