Mqtt-in node not connecting to broker

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.

Must check the mosquito config file.
Check this

The listener is the key point to solve your issue

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

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

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...

What IP have you configured the mqtt node with?

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...

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

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.

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

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
//////
///

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

Thank you, all for your willingness to help !

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