# Mqtt-in node not connecting to broker

**URL:** <https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375>\
**Category:** General\
**Created:** [5 August 2021 10:10 UTC](https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375 "2021-08-05T10:10:39Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![donblago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/donblago/32/46190_2.png) [@donblago](https://discourse.nodered.org/u/donblago)\
**Post date:** [5 August 2021 10:10 UTC](https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375/1 "2021-08-05T10:10:39Z")

</div>

Hello,

I am just starting with Node-red and got to the point of not able to connect to MQTT broker

NODE\_RED\_VERSION :vv2.0.5  
NODE\_VERSION :14.17.3

HW:PI4b  
Issue: MQTT node not connecting  
Issue: 5 Aug 09:40:35 - [info] [mqtt-broker:mqtt broker] Connection failed to broker: mqtt://localhost:1883"

My storyline; I have started with simple IOT stack in Docker (default portainer-ce, node-red, mosquitto, influxDB, Grafana) .

I did testing on the Mosquitto;

1. enter Mosquitto container via Terminal  
$sudo docker exec -it mosquitto sh
2. set topic "test" for the broker  
$ mosquitto\_sub -d -t test

In second Terminal

1. enter Mosquitto container via Terminal  
$ sudo docker exec -it mosquitto sh
2. publish test message on topic "test" for the broker  
$ mosquitto\_pub -d -t test -m "How are you"

All worked and still working fine!

Second test was in Node-red - deploying simple flow is Mosquitto-in node -\> Debug node. Initially I was able to connect to the Mosquitto broker (I have defined only one broker) . Then I have introduced user and pass. I was still able to connect, but this time with credentials.... all good.

Third test was with InfluxDB. At the point I introduce the InfluxDB node in the flow, my MQTT broker was not able to connect any more.

I have removed the InfluxDB node from the flow , but still not able to connect. I have re-installed the mosquitto container, and also the node-red container ... but no success with making connection with the broker.

Last try I did was to remove node-red container with the help of portainer-ce and get fresh (latest node-red image) from Node-red GIthub...

Node-red is starting properly ... Portainer is reporting healthy status .... but some how can't make connection with the mosquitto broker - when flow is deployed, the mosquitto node show disconnected.

Can you give me a hint how to get more verbose logs on what is happening when Node-red is trying to connect with the Mosquitto broker?

I am able to make the Mosquitto SUB & PUB test via terminal. Probably the issue is me not grasping good enough the Node-red concept.

---

<div class="post-metadata">

**Author:** ![JoseGodinho](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@JoseGodinho](https://discourse.nodered.org/u/JoseGodinho)\
**Post date:** [5 August 2021 11:42 UTC](https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375/2 "2021-08-05T11:42:53Z")

</div>

Must check the mosquito config file.  
Check this

> **[Migrating from 1.x to 2.0](https://mosquitto.org/documentation/migrating-to-2-0/)**
>
> Introduction
> Listener behaviour changes
> Use of root/privileged user
> Other behaviour
> Packaging and Distribution
> Plugins
> 
> 
> Introduction
> Mosquitto 2.0 introduces a number of changes to the behaviour of t

The listener is the key point to solve your issue

---

<div class="post-metadata">

**Author:** ![donblago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/donblago/32/46190_2.png) [@donblago](https://discourse.nodered.org/u/donblago)\
**Post date:** [5 August 2021 12:47 UTC](https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375/3 "2021-08-05T12:47:31Z")

</div>

Thank you for sharing!  
But I am running the default Mosquitto vertion from IOT-stack and this is 1.6.12

My Mosquitto conf file is default-ed (like after fresh installation) with no authentication.

I am doing loop test on the host... my understandings are that this should be fine

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [5 August 2021 13:15 UTC](https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375/4 "2021-08-05T13:15:39Z")

</div>

And you have configured container networking so they can see each other ? (Or used docker compose to create them all ?)

---

<div class="post-metadata">

**Author:** ![donblago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/donblago/32/46190_2.png) [@donblago](https://discourse.nodered.org/u/donblago)\
**Post date:** [5 August 2021 19:32 UTC](https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375/5 "2021-08-05T19:32:13Z")

</div>

Yes. I can see Mosquitto got IP 10.77.60.2/24 and Node-red IP 10.77.60.3/24, both connected to "IOTstack\_Net" network. It seems the network is configured properly.

This is snippet from my docker-compose yaml file  
services:  
mosquitto:  
container\_name: mosquitto\_broker  
image: eclipse-mosquitto  
restart: unless-stopped  
environment:  
- TZ=Etc/UTC  
ports:  
- "1883:1883"  
volumes:  
- "/home/pi/Mosquitto/config:/mosquitto/config"  
- "/home/pi/Mosquitto/data:/mosquitto/data"  
- "/home/pi/Mosquitto/log:/mosquitto/log"  
- "/home/pi/Mosquitto/pwfile:/mosquitto/pwfile"  
networks:  
- iotstack\_nw

and here is for NOde-red

nodered:  
container\_name: nodered  
image: nodered/node-red  
restart: unless-stopped  
user: "0"  
environment:  
- TZ=Etc/UTC  
ports:  
- "1880:1880"  
volumes:  
- ./volumes/nodered/data:/data  
- ./volumes/nodered/ssh:/root/.ssh  
- /var/run/docker.sock:/var/run/docker.sock  
- /var/run/dbus/system\_bus\_socket:/var/run/dbus/system\_bus\_socket  
devices:  
- "/dev/ttyAMA0:/dev/ttyAMA0"  
- "/dev/vcio:/dev/vcio"  
- "/dev/gpiomem:/dev/gpiomem"  
networks:  
- iotstack\_nw

the network itself is defined as:  
networks:  
iotstack\_nw:  
# external: true  
name: IOTstack\_Net  
driver: bridge  
ipam:  
driver: default  
config:  
- subnet: 10.77.60.0/24

Following the logic...If Mosquitto works on its own (SUB and PUB).... but not with node-red .... must be network definition issue. I will dive into it...

---

<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:** [6 August 2021 07:10 UTC](https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375/6 "2021-08-06T07:10:21Z")

</div>

What IP have you configured the mqtt node with?

---

<div class="post-metadata">

**Author:** ![donblago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/donblago/32/46190_2.png) [@donblago](https://discourse.nodered.org/u/donblago)\
**Post date:** [6 August 2021 08:00 UTC](https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375/7 "2021-08-06T08:00:19Z")

</div>

Hello Colin,

At the MQTT node I have set the localhost IP (in my case it is 192.168.1.3). I am following various tutorials from the net, all say it must be the IP of you host (RPI4 in my case). The 10.77.60.2 i assume is IP used only for communication between containers, right?

Very strange issue. Everything seems to be done by the book...

I wonder now, is it possible that specific version Mosquitto with specific version of Node-red can get along ... in case of deployment over Docker...

---

<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:** [6 August 2021 08:08 UTC](https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375/8 "2021-08-06T08:08:32Z")

</div>

Isn't it quicker to try it rather than ask?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 August 2021 08:15 UTC](https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375/9 "2021-08-06T08:15:15Z")

</div>

I suspect you are using wrong IP. The IP to set in the MQTT config in node-red should be the IP of where you install the broker.

In your case, I believe you used a container, not directly on the PI. The IP of the container where mosquito is installed should be the address you use in an MQTT client (I.e. node-red MQTT config node).

Use a client like MQTT explorer and get it to connect across your LAN to your broker. Then copy settings to node-red MQTT config node.

---

<div class="post-metadata">

**Author:** ![donblago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/donblago/32/46190_2.png) [@donblago](https://discourse.nodered.org/u/donblago)\
**Post date:** [6 August 2021 08:17 UTC](https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375/10 "2021-08-06T08:17:59Z")

</div>

I have tried .... it is not working!  
I am asking .... to double check if my thinking is in right direction...

---

<div class="post-metadata">

**Author:** ![donblago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/donblago/32/46190_2.png) [@donblago](https://discourse.nodered.org/u/donblago)\
**Post date:** [6 August 2021 08:28 UTC](https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375/11 "2021-08-06T08:28:23Z")

</div>

Hello Steve,

That's correct, Mosquitto and Node-red are docker containers (in my case)  
I have try with setting the Mosquitto IP as parameter of the Mosquito node, but still does not connect.

In the config file (mosquitto.conf) I don't have any settings setting the IP. It is assigned dynamic at the time container starts.

here is my mosquitto.conf file:  
///  
///  
///  
listener 1883

/// persistence enabled for remembering retain flag across restarts  
persistence true  
persistence\_location /home/pi/Mosquitto/data

/// logging options:  
log\_dest stdout  
log\_timestamp\_format %Y-%m-%dT%H:%M:%S

/// password handling:  
allow\_anonymous true  
//////  
///

---

<div class="post-metadata">

**Author:** ![donblago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/donblago/32/46190_2.png) [@donblago](https://discourse.nodered.org/u/donblago)\
**Post date:** [6 August 2021 13:27 UTC](https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375/12 "2021-08-06T13:27:38Z")

</div>

Okay, at the end ....I was able to get both Mosquitto broker and Node-red in one network... and by replacing "localhost" parameter in the Mosquitto node with virtual IP of the Mosquitto broker (as Steve-Mcl suggested) .... connection was established.

Just for anyone who may end-up willing to understand docker networking:  
Youtube video - **Docker Advanced Networking** , 10min long

[![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/53f5b2bb0c47d437ac50f19477c3ab9fe19a033b.jpeg "Docker Advanced Networking - The Secret to Optimizing Your Container Logging") ](https://www.youtube.com/watch?v=Xxhhdo2e-DA)

Thank you, all for your willingness to help !

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 August 2021 13:27 UTC](https://discourse.nodered.org/t/mqtt-in-node-not-connecting-to-broker/49375/13 "2021-08-20T13:27:45Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
