# NodeRed MQTT bridge?

**URL:** <https://discourse.nodered.org/t/nodered-mqtt-bridge/55308>\
**Category:** General\
**Created:** [16 December 2021 17:39 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308 "2021-12-16T17:39:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [16 December 2021 17:39 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/1 "2021-12-16T17:39:49Z")

</div>

Does anyone know of a flow to do this? I can't do it in Mosquitto (via the broker itself) unfortunately as there's some bug which means the bridge keeps disconnecting.

I wondered if it could be done easily in Node Red?

I found an example online for an Adafruit one but it's not what it appears to be.

many thanks!!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 December 2021 18:14 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/2 "2021-12-16T18:14:52Z")

</div>

> [@dclear](#):
>
> there's some bug which means the bridge keeps disconnecting.

Is that a documented bug? If not then I suspect it is not a bug. I don't have any such problems.

What do you see in the mosquito log?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 December 2021 18:45 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/3 "2021-12-16T18:45:31Z")

</div>

You will have to elaborate about what functionality you are looking for.

Node-RED can certainly subscribe to topics on one broker and publish them to another one.

But it would also be good to understand what bug you're hitting in mosquitto.

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [16 December 2021 21:57 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/4 "2021-12-16T21:57:36Z")

</div>

As anything that would likely cause the broker to be disconnected from it's paired broker would also likely disconnect the "outbound" connection from Node-RED as well.

But also MQTT bridges can make use of QOS 1 or 2 to ensure that no messages are missed if there is a disconnection event (just delayed until the connection is re-established)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [16 December 2021 22:13 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/5 "2021-12-16T22:13:28Z")

</div>

> [@dclear](#):
>
> I can't do it in Mosquitto (via the broker itself) unfortunately as there's some bug which means the bridge keeps disconnecting.

I'm doing it with Mosquitto, I forward the `$SYS` messages from Mosquitto on my old Pi2 to my live home server (converting the source `$SYS/#` to the target `services/pi2/mosquitto/#` topic). It is working well and loads of data gets sent every 10s.

Currently showing me that the Pi's broker has been up for 15372066 seconds 🙂 Around 178 days if I worked it out correctly.

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [17 December 2021 08:51 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/6 "2021-12-17T08:51:17Z")

</div>

> [@Colin](#):
>
> What do you see in the mosquito log?

Socket error (Bridge)  
Reconnect (Bridge)  
Socket error (Bridge)

All the while, the normal clients stay connected

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [17 December 2021 08:59 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/7 "2021-12-17T08:59:13Z")

</div>

Thanks

Simply looking to republish topics and payloads from one MQTT broker to another (across a VPN)

I'm hitting a bug with Mosquitto v2 (I know it's old) that means when I use the following config for the bridge

```auto
connection bridge-01
address 192.168.1.2:1883
topic # out 0
topic # in 0

```

or

```auto
connection bridge-01
address 192.168.1.2:1883
topic # out 0
topic # in 0
try_private false
notifications false
bridge_attempt_unsubscribe true

```

I get the connection attempt then an immediate socket error

I'm using Mosquitto v2 because I neither want nor need all the various security that later versions more or less insist on. I've tried a few times to upgrade and turn off the security but each time my MQTT totally fell over and I was unable to configure it to work unauthenticated like with Mosquitto v2. I've posted about it [here](https://community.home-assistant.io/t/attempting-to-bridge-two-mqtt-brokers/367288) as well, but so far no response. Which is fine. Nobody's obligated to respond to me! 😃

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [17 December 2021 09:16 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/8 "2021-12-17T09:16:20Z")

</div>

In my setup, I _pull_ from the Pi so the config is on my main server. I added a new file to `/etc/mosquitto/conf.d/` with a name starting with `z` so that it sorts last and other conf's load first.

```auto
connection pi2-mosquitto
address pi2.xxxxxxxxxx
cleansession true
notifications true
topic "" in 0 ESP/D1M02 HARDWARE-IN/WIFI/D1M02
topic # in 0 services/pi2/mosquitto/ $SYS/broker/
topic "" in 0 telegraf/pi2/ telegraf/pi2/

```

As you can see, three topic tree's are pulled, each being translated into a new tree in the target. Output from an old ESP8266 device (no longer needed actually, just not got round to changing), The source `$SYS` tree and the source telegraf tree which is system stats produced by telegraf on the Pi.

I do not include a port number in the address.

My main server is running Mosquitto v2.0.12 but the Pi is running v1.6.12 as it doesn't get a lot of love these days, just keeps on rocking.

By the way, v2 **IS** the latest version.

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [17 December 2021 09:23 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/9 "2021-12-17T09:23:44Z")

</div>

That's great. But it doesn't seem to like that option at my end, hence wondering about a lovely NodeRed option! 🙂

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [17 December 2021 09:24 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/10 "2021-12-17T09:24:44Z")

</div>

> [@TotallyInformation](#):
>
> By the way, v2 **IS** the latest version.

ah sorry - I'm running it as an add-on to Home Assistant, and there the latest version is 6 something

> [@TotallyInformation](#):
>
> In my setup, I _pull_ from the Pi so the config is on my main server. I added a new file to `/etc/mosquitto/conf.d/` with a name starting with `z` so that it sorts last and other conf's load first.
> 
> ```auto
> 
> ```

Thanks I'll try it from the opposite end then! 🙂

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 December 2021 09:26 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/11 "2021-12-17T09:26:02Z")

</div>

> [@dclear](#):
>
> I'm hitting a bug with Mosquitto v2

V2 isn't old, that is the current version.

If you do it without bridging then add an MQTT In node subscribed to the remote topics and connect it an MQTT node publishing to the local one. Also add a pair the other way round. Make sure you don't have the same topic in both directions in that case though, or you will get a loop.

Does bridging cope with specifying the same topics in both directions? I have never tried it. I always want one broker to be the master for a particular topic so I make the it clear in the bridge definition which direction any particular one travels.

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [17 December 2021 09:29 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/12 "2021-12-17T09:29:30Z")

</div>

Home Assistant seem to insist on having their own version numbers for things they include that mask the real underlying version number. We see it with people using their Node-RED container as well. It makes it very hard to work out what the actual version people are running is.

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [17 December 2021 09:31 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/13 "2021-12-17T09:31:08Z")

</div>

> [@Colin](#):
>
> If you do it without bridging then add an MQTT In node subscribed to the remote topics and connect it an MQTT node publishing to the local one. Also add a pair the other way round. Make sure you don't have the same topic in both directions in that case though, or you will get a loop.

I think this is my problem - I've tried just connecting an "in" node to an "out" node (obviously set to the different brokers) but it mangles the topics. That's the reason for this thread.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 December 2021 09:35 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/14 "2021-12-17T09:35:10Z")

</div>

> [@dclear](#):
>
> I've tried just connecting an "in" node to an "out" node (obviously set to the different brokers) but it mangles the topics

What do you mean it mangles the topics?

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [17 December 2021 09:40 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/15 "2021-12-17T09:40:47Z")

</div>

Sorry, As in doesn't "relay" them, they aren't mirrored. I'll try to post an example

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [17 December 2021 09:41 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/16 "2021-12-17T09:41:20Z")

</div>

hmmm, so I tried your config, and this is what I got

```auto
1639733750: mosquitto version 1.4.15 (build date 2018-03-04 15:36:00+0000) starting
1639733750: Config loaded from /etc/mosquitto.conf.
1639733750: Opening ipv4 listen socket on port 1883.
1639733750: Opening ipv6 listen socket on port 1883.
1639733750: Warning: Mosquitto should not be run as root/administrator.
1639733750: Connecting bridge homehass (vin.duckdns.org:1883)
11639733772: New connection from 172.3.3.1 on port 1883.
1639733772: New client connected from 172.3.3.1 as 5ALWSarEytsChZ4R (c1, k60, u'homeassistant').
1639733810: Socket error on client local.core-mosquitto.homehass, disconnecting.
1639733841: Connecting bridge homehass (vin.duckdns.org:1883)
1639733901: Socket error on client local.core-mosquitto.homehass, disconnecting.

```

I'm wondering if it's an SSL thing? Both of my Home Assistant servers use SSL

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 December 2021 09:45 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/17 "2021-12-17T09:45:16Z")

</div>

> [@dclear](#):
>
> doesn't "relay" them

Obviously make sure you have the topic blank in the MQTT Out so that it will use the topic in the message.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [17 December 2021 09:54 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/18 "2021-12-17T09:54:08Z")

</div>

> [@dclear](#):
>
> running it as an add-on to Home Assistant

Hmm....

> [@Colin](#):
>
> Does bridging cope with specifying the same topics in both directions? I have never tried it. I always want one broker to be the master for a particular topic so I make the it clear in the bridge definition which direction any particular one travels.

Yes, see my config. You can have the exact same topic tree or you can change the root topic as I do.

I use it to bridge from an old setup that I've not quite gotten round to decommissioning yet to my current home automation server.

> [@dclear](#):
>
> `Socket error`

Are you using websockets on the broker? Not sure whether that message is simply unclear or whether that is part of the problem.

> [@dclear](#):
>
> `vin.duckdns.org`

Why are you using this? That means that traffic is leaving your local network - is that what you want?

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [17 December 2021 10:13 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/19 "2021-12-17T10:13:57Z")

</div>

> [@TotallyInformation](#):
>
> Are you using websockets on the broker? Not sure whether that message is simply unclear or whether that is part of the problem.

MQTT client disconnects due to errors always say "socket error" don't think it's related to websockets.

> [@TotallyInformation](#):
>
> Why are you using this? That means that traffic is leaving your local network - is that what you want?

Yeah like I said it's a VPN connection - between two sites

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [17 December 2021 10:27 UTC](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308/20 "2021-12-17T10:27:03Z")

</div>

Got the Node-Red bridge working - thanks.

```auto
[
    {
        "id": "fce60d48.6e19b",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": ""
    },
    {
        "id": "f619cb6.40bac38",
        "type": "mqtt in",
        "z": "fce60d48.6e19b",
        "name": "h Hass",
        "topic": "iotlink/#",
        "qos": "0",
        "datatype": "auto",
        "broker": "70556b7.be24694",
        "x": 410,
        "y": 360,
        "wires": [
            [
                "97493c3c.40ef5"
            ]
        ]
    },
    {
        "id": "97493c3c.40ef5",
        "type": "mqtt out",
        "z": "fce60d48.6e19b",
        "name": "p Hass",
        "topic": "",
        "qos": "",
        "retain": "",
        "broker": "af7f0536.34c678",
        "x": 600,
        "y": 360,
        "wires": []
    },
    {
        "id": "70556b7.be24694",
        "type": "mqtt-broker",
        "z": "",
        "name": "homehass",
        "broker": "192.168.1.2",
        "port": "1883",
        "clientid": "nodered",
        "usetls": false,
        "compatmode": false,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    },
    {
        "id": "af7f0536.34c678",
        "type": "mqtt-broker",
        "z": "",
        "name": "whass",
        "broker": "192.168.1.3",
        "port": "1883",
        "clientid": "node-red",
        "usetls": false,
        "compatmode": false,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    }
]

```

[Next page](https://discourse.nodered.org/t/nodered-mqtt-bridge/55308.md?page=2)
