How-to
Bridge a local Mosquitto broker to a cloud broker
Short answer
Add a bridge block to the local mosquitto.conf so the on-site broker dials the cloud broker over TLS on 8883 and forwards site/state/# out and site/cmd/# in at QoS 1. The connection is outbound only, so no port forward or DDNS is needed, and cleansession false keeps QoS 1 messages queued through uplink outages.
On this page
Add a bridge block to the local broker's mosquitto.conf and let the on-site broker dial the cloud broker over TLS on 8883. The site opens the connection, so nothing on the internet ever calls in: no port forward, no DDNS record, no inbound firewall rule. The local Node-RED keeps running every flow while the uplink is down, state flows out on one topic prefix, commands flow in on another, both at QoS 1, and cleansession false holds a persistent session so QoS 1 messages published during an outage queue up and flush on reconnect. Below: the topic contract, the config block, the queue arithmetic for a planned 30-minute outage, the symptom table and the decision for each case.
One outbound session replaces every inbound rule #
A bridge is a broker-to-broker feature: the local Mosquitto broker creates its own client connection to the cloud broker and forwards exactly the topic patterns you list in its configuration. Because the local side initiates the session, the cloud broker never needs a route into the plant, and the site can sit behind CGNAT or a rotating address with no effect on the design. Only one hop crosses the uplink: a browser talks to a cloud Node-RED instance over HTTPS, that instance talks to the cloud broker, the cloud broker holds the bridge session the local broker opened, and the local Node-RED talks to the local broker over loopback. When the uplink dies, that session drops and the other three hops keep running.
Branch on what you actually need. If nothing outside the plant must ever reach the site, stop here: a bridge solves nothing, so give the edge host stable power and working DNS and let its outbound API calls ride the uplink. The bridge exists for the other case, where a user or a system outside the plant must see state and issue commands while the site stays autonomous.
The local broker keeps serving its local clients exactly as before, so harden its listener and access rules first if you have not: the secure Mosquitto MQTT lab broker setup with Node-RED covers the listener, TLS and ACL discipline that both ends of this design should carry.
Split the flows so the site runs without the internet #
Node-RED has no flow mirroring between instances, so mirror the data, not the flows. Build every dashboard so it reads state from and writes commands to MQTT topics only, with no direct dependency on local-only nodes for the values it shows. Run identical UI tabs on the local and the cloud instance, and keep device I/O, control logic and local API calls on the local instance only: export the same flow file to the cloud copy and disable the tabs it must not run twice. Each instance points at its own nearest broker, and the bridge is the only thing that synchronizes them.
That is the same publish-versus-control separation as keeping publish and control separate in a Node-RED IO data MQTT setup: state prefixes carry values out of the site, command prefixes carry writes in, and the two prefixes never overlap, because an overlap is what lets a message echo between brokers.
One gate belongs in this design: a QoS 1 command published while the uplink is down is delivered the moment the bridge reconnects, so command execution at the site must sit behind an enable switch or a freshness check that refuses stale commands, or a queued write can actuate equipment long after the operator believed it had failed.
Configure the bridge in mosquitto.conf #
- Provision the cloud broker with TLS on its listener, port 8883, and create an account only this bridge uses, with its ACL restricted to the site prefix, for example
site1/#. - Copy the CA certificate that signs the cloud listener certificate onto the local broker, for example to
/etc/mosquitto/certs/cloud-ca.crt. - Add one bridge block to the local broker's
mosquitto.conf, using the block below. The address, credentials, client ID and CA path are example values; use the ones the cloud service issued. - Publish state at QoS 1 with the retain flag set in the mqtt out node, and publish commands at QoS 1 from the cloud side. QoS 0 messages are never queued for an offline client, so anything that must survive an outage needs QoS 1.
- Point the local Node-RED configuration nodes at the local broker and the cloud instance's nodes at the cloud broker; neither instance knows the bridge exists.
- Restart the local broker and read its log for the bridge connect and subscribe lines, and leave notifications true so the bridge announces its state to the cloud broker.
The block for step 3:
connection site1-to-cloud
address cloud-broker.example.com:8883
bridge_cafile /etc/mosquitto/certs/cloud-ca.crt
remote_username site1-bridge
remote_password site1-bridge-password
clientid site1-bridge
cleansession false
try_private true
notifications true
topic site1/state/# out 1
topic site1/cmd/# in 1
The third field on each topic line is the direction: out forwards local publishes to the cloud broker, in subscribes the cloud side for the local broker, and both does the two together, which you leave off state and command prefixes unless you have a specific reason. The trailing 1 on each line sets the QoS the bridge uses for that pattern, and the address line carries the port, so a cloud service listening elsewhere is a one-field change. try_private true tells a Mosquitto peer that this connection is a bridge, which is what suppresses echoes between the two brokers; keep it true between Mosquitto peers and set it to false only if a non-Mosquitto cloud service refuses the bridge handshake, in which case the separated prefixes carry the loop prevention. If any flow publishes QoS 2, a cloud service that only supports QoS 1 delivers those messages downgraded, which is its own subject: see resolving MQTT QoS 2 downgrade on QoS 1-only brokers.
Size the offline queue with the outage you plan for #
With cleansession false the bridge holds a persistent session, which turns an outage from data loss into delay: QoS 1 messages published on out-topics while the uplink is down queue at the local broker and flush in order on reconnect, and QoS 1 messages published on in-topics while the site is offline queue at the cloud broker for delivery on reconnect the same way. Size the queue with your publish rate and the outage you accept, not with assumptions.
Worked example. A site publishes 20 machine state topics, each republished every 5 s, which is 20 / 5 = 4 messages/s. Plan for the longest uplink outage you accept, here 30 min = 1,800 s. The outage produces 4 x 1,800 = 7,200 queued messages. At an example average of 512 bytes per message, the queue holds 7,200 x 512 = 3,686,400 bytes, about 3.7 MB. The same site through a 2-hour outage produces 4 x 7,200 = 28,800 messages and 28,800 x 512 = 14,745,600 bytes, about 14.7 MB.
Count publishes, not topics: while the bridge is offline, every republished retained value is its own queue entry, so a fast republish rate grows the queue even when the values change rarely. The broker bounds its queues with its own limit settings; read them in the same manual as the bridge options and set them against the arithmetic above.
Read the symptoms when the bridge misbehaves #
| Observation | Likely cause | What to read |
|---|---|---|
| Local UI works, the cloud shows stale values | Bridge session down: uplink, DNS or a NAT timeout | The local broker log for the disconnect line, and the retained status topic on the cloud broker |
| Bridge connects, then drops immediately | CA or certificate mismatch, wrong credentials, or a second process using the same client ID | The connect-time reason in the broker log, and that only one copy of the bridge runs |
| Bridge is up, the cloud receives nothing | Topic pattern or direction wrong, or the cloud ACL denies the bridge account | A test subscription on the cloud broker, and the bridge account's ACL |
| A cloud command never reaches the site | The command prefix is not bridged in, or the cloud side published at QoS 0 | The direction field of that topic line, and the QoS of the publishing node |
A handshake that dies at connect time is usually a trust failure; work the certificate and CA checks from fixing a Mosquitto MQTT TLS listener that returns unknown error, which apply to the bridge client on the local side the same way.
The bridge queue covers broker-to-broker forwarding only. If the local flow also buffers its own writes to a historian or MES while a different path is down, that buffering lives in the Node-RED layer, and MQTT store and forward backfill after a connection outage covers how that backlog drains once the path returns.
The decision for each case #
- Only outbound calls from the site: no bridge and no tunnel. Stable power, working DNS, done.
- Remote visibility with local autonomy on hardware that cannot carry a tunnel agent: the bridge as configured, state out at QoS 1 with retain set, commands in at QoS 1,
cleansession false, and the queue sized to your publish rate times your longest accepted outage. - Cloud side must run logic the site cannot: the same bridge plus a cloud Node-RED instance that runs only its own tabs; copy the UI flows across and disable the tabs that must not run twice.
- IT-managed router at the site: nothing inbound exists to request. Ask IT for one outbound TLS session from the broker host to the cloud broker address on port 8883, get the egress approval, deploy.
Questions asked
Does the bridge work when the site sits behind CGNAT or a changing address?
Yes. The local broker opens the TLS session outbound to the cloud broker, so the cloud side never dials the site and the site address does not matter. You need no DDNS record, no port forward and no inbound rule; the only approval you may have to ask for is IT permission for one outbound connection to the cloud broker on 8883.
What happens to QoS 2 messages on a cloud broker that only supports QoS 1?
They arrive downgraded to QoS 1, with the duplicate handling that goes with it, so a publishing flow cannot assume the QoS 2 guarantees survived the trip. Keep the topics that must survive an outage at QoS 1 on both the bridge topic lines and the publishing nodes, and treat QoS 0 as fire-and-forget, because QoS 0 is never queued for an offline client.
Can two Mosquitto brokers mirror every topic automatically?
No. A bridge forwards exactly the patterns you list, each with its own direction and QoS, and a topic added later stays local until you add its pattern and reload the broker configuration. To mirror a whole site, list every prefix deliberately, keep state and command prefixes apart, and use both only where a pattern genuinely belongs to both sides.
Why set the retain flag on state messages?
Retain is a per-message property set in the publishing node, and the bridge forwards the flag with the message. The cloud broker then keeps the last value per topic and hands it to every new subscriber immediately, so a dashboard that reconnects after an outage shows current values at once instead of waiting for the next publish cycle.
How do you monitor the bridge while the site is unreachable?
Leave notifications true and the bridge publishes a retained connection-state value to a status topic on the cloud broker, one value for connected and one for disconnected. Run a small monitoring client or webhook on the cloud side that subscribes to that topic, and you get an alarm path that keeps working precisely when the site itself cannot be reached.