Mosquitto MQTT Bridge Error - mqtt

I am trying to bridge my local (Windows) MQTT mosquitto broker to test.mosquitto.org. Unfortunately it raises a Unknown Error.
I am using the same configuration that is used in this question
Verify that local mosquitto MQTT Broker is bridged to test.mosquitto.org
Config file:
connection test
address test.mosquitto.org
topic oust_topicst_topic out 0
try_private false
notifications false
bridge_attempt_unsubscribe true
Output:
1489747961: mosquitto version 1.4.11 (build date 20/02/2017 23:24:29.40) starting
1489747961: Config loaded from Configurations/bridge.conf.
1489747961: Opening ipv6 listen socket on port 1883.
1489747961: Opening ipv4 listen socket on port 1883.
1489747961: Bridge local.NicolasJourdan.test doing local SUBSCRIBE on topic oust_topicst_topic
1489747961: Connecting bridge test (test.mosquitto.org:1883)
1489747961: Bridge NicolasJourdan.test sending CONNECT
1489747961: Error creating bridge: Unknown error.
1489747961: Warning: Unable to connect to bridge test.
1489747969: mosquitto version 1.4.11 terminating
What could produce this error? I am not running another instance of mosquitto and test.mosquitto.org is up

First question is whether you are running a local broker or just connecting as a client.
If you are running a broker try to first run mosquitto with -c "path to config file" to see whether your config file has an error in it. Run in terminal (example on linux):
mosquitto -c /etc/mosquitto/mosquitto.conf
If there is an error the console will show it and you take it from there.
Next, try to format your topics like this (it is called topic remapping):
topic test out 0 local/ local/
Now you can publish to local/test from your code or terminal, and the message will be published to local/test on the remote broker, which will receive messages by suscribing to the same topic.

Update September 2019
The bug on Windows as described below has reportedly been fixed in version 1.6.5 of Mosquitto.
There appears to be a bug with Mosquitto running on Windows that prevents bridging from working.
On Windows the socket is not ready before Mosquitto attempts to connect.
See https://github.com/eclipse/mosquitto/issues/478 and https://github.com/eclipse/mosquitto/issues/580. This describes the underlying issue on Windows:
After checking the code I found that the statement in bridge.c .... makes a non blocking socket connect.
...
So the the next command ... sometimes fails, since the connection has not been established, as the previous connect is non-blocking.

Related

MQTT connection refused when trying to connect from remote machine

I am trying to connect to a mosquitto broker, across linux clients. I can get everything working from the local machine, but when trying to connect from another machine I get the error ConnectionRefusedError: [Error 111] Connection refused.
Here is the process:
On the local machine, I install mostquitto, stop the service and start a live instance:
#Terminal 1
sudo service mosquitto stop
mosquitto
I then try and pub and sub, from distinct terminals on that machine:
#Terminal 2
mosquitto_sub -t 'test'
#Terminal 1 shows new connection
#Terminal 3
mosquitto_pub -t 'test' -m 'Hello, world!'
#Terminal 1 shows new connection, and then disconnect.
#Terminal 2 shows 'Hello, world!'
I now try to connect from a remote machine. First I edit the mosquitto config file to allow unauthorized connections:
sudo nano /etc/mosquitto/mosquitto.conf
#Add the following:
listener 1883
allow_anonymous true
protocol mqtt
I note that the mosquitto logs previously showed only local connections allowed, after editing the config file and restarting, the logs no longer show that message.
Then I install paho-mqtt on another machine. I run the following python script:
import paho.mqtt.client as mqtt
client = mqtt.Client('131')
client.connect('192.168.0.146') #The IP of machine 1, running the broker where the code above ran correctly across the terminals
I get the error mentioned above:ConnectionRefusedError: [Error 111] Connection refused. The mosquitto instance on machine 1 shows nothing. Logs show nothing.
I can't work out what is going on. I have read every question on SO that I can find. Nothing goes beyond changing the config file. I have tried running the broker on two machines (laptop and pi). I have tried connecting from multiple different sources: esp32 board, different laptop and pi. Nothing works. I can only assume there is some network-wide problem, but my network isn't isolating devices as I ssh into my pi all the time and have wifi lights and switches running on the LAN.
If anyone can help me troubleshoot I would be very grateful.
Mosquitto will not pick up a default configuration file, you must always pass the configuration file with the -c command line argument or it will fall back to the baked in config (that will only listen on localhost)
The service includes -c /etc/mosquitto/mosquitto.conf to force it to use the config file.

Publishing or subscribing data via external IP

I am running Mosquitto MQTT broker on the localhost of my PC and I want to publish or subscribe data through MQTT with Mosquitto from a remote IP (another PC). Both the PCs are connect to the same Wifi public network. I have disable the firewall of the two PCs and I am able to ping one another on command prompt. I have look up on several websites and tried editing the config file of the Mosquitto MQTT broker by adding these two lines.
listener 1883
allow_anonymous true
I have started the Mosquitto broker by executing mosquitto -c mosquitto.conf -v
The following is the log appear the mosquitto start.
C:\Program Files\mosquitto>mosquitto -c mosquitto.conf -v
1665133477: mosquitto version 2.0.15 starting
1665133477: Config loaded from mosquitto.conf.
1665133477: Opening ipv6 listen socket on port 1883.
1665133477: Opening ipv4 listen socket on port 1883.
1665133477: mosquitto version 2.0.15 running
However, on the publishing PC, it prompts the error below.
C:\Program Files\mosquitto>mosquitto_pub -h XXX.XXX.XX.X -t Test -m "Hello World" -p 8883 -d
Error: No connection could be made because the target machine actively refused it.
The situation is same for subscribing. I am currently using the latest version of Mosquitto 2.0.15. Can anyone help me on this situation? Your help would be much appreciated. Thanks in advance!

How to make the connection between the broker and an external network?

I’m new in this field and I’m trying to create a device using arduino WiFi Rev 2 and the broker mqtt Mosquitto on an external network.
I’ve already installed Mosquitto on my PC and started it by the prompt. I changed the configuration file of Mosquitto in order to allow the listener on port 1883 and then run on prompt the command mosquitto -c mosquitto.conf -v and it worked.
But when I run netstat-a on the prompt, the port 1883 doesn’t appear on the address of the external network. What can I do to fix it ?
There’s the conf file I added
per_listener_settings true
listener 1883
protocol mqtt
allow_anonymous true
listener 8083
protocol websockets
allow_anonymous true
Thanks for the your attention, hope to find a solution!
With the configuration you have provided, mosquitto will bind to ALL addresses, this will be signified by it binding to 0.0.0.0:1883 (and 0.0.0.0:8083)
So there is nothing to fix. Assuming no firewall/NAT between the Arduino and the machine running the broker it should just connect.

Mosquitto - EMQX bridge connection name

1) How to determine the Mosquitto bridge connection name.
The snippet written below is from the EMQx documentation (http://emqtt.io/docs/v2/bridge.html#mosquitto-conf)
connection emqttd
address 127.0.0.1:2883
topic sensor/# out 2
the connection name - emqttd is written on what basis?
2) I have a tcp connection open from EMQX on the host 172.22.25.53 and port 2883.
When i try connecting from the same system. It works absolutely fine. But from a different system, I get this error
C:\Program Files\mosquitto>mosquitto.exe -c mosquitto.conf -v
1542182003: mosquitto version 1.5.3 starting
1542182003: Config loaded from mosquitto.conf.
1542182003: Opening ipv6 listen socket on port 1883.
1542182003: Opening ipv4 listen socket on port 1883.
1542182003: Bridge local.AOI146.emq#172.22.25.53 doing local SUBSCRIBE on topic BA/#
1542182003: Connecting bridge emq#172.22.25.53 (172.22.25.53:2883)
1542182003: Bridge AOI146.emq#172.22.25.53 sending CONNECT
1542182003: Error creating bridge: Unknown error.
1542182003: Warning: Unable to connect to bridge emq#172.22.25.53.
My mosquitto.conf file is:
connection emqx
address 172.22.25.53:2883
cleansession false
topic AB/# both 2
The connection name is the value after connection in the configuration file. As described in the bridge section of the mosquitto.conf man page
e.g.
connection foo
Will result in a connection name of foo.
172.22.25.53 is in the reserved range 172.16.0.0 – 172.31.255.255, I'm going to guess you are running emq in a docker container and you are trying to access the container using it's Docker internal IP address. You will need to expose the port on your host machine to be able to access it from a remote machine.

Verify that local mosquitto MQTT Broker is bridged to test.mosquitto.org

I have a local (OSX 11) mosquitto broker (1.4.7) running with the following config:
connection test
address test.mosquitto.org
topic in_topic in 0
topic out_topic out 0
try_private false
notifications false
bridge_attempt_unsubscribe true
I got the conf settings form here: bridge local mosquitto to cloud broker (thanks)
I now have 3 terminals.
A: subscribed to test.mosquitto.org directly using a ruby script and ruby-mqtt.
B: subscribed to local broker with mosquitto_sub -t in_topic
C: publishes using this command: mosquitto_pub -h test.mosquitto.org -t in_topic -m "hello world1"
Now, when I do this I get the response in A and B so that makes me think my bridging connection is working. However, if I drop the -h test.mosquitto.org I don't get the message from A. So, the message is only going to that broker (test.mosquitto.org) if I include the -h option.
On the other hand, if I stop the local mosquitto broker and then run the same command including the -h option then the A still gets the message and B doesn't. B actually doesn't even start up since the connection is refused since local broker is not running.
So, my question is, does this configuration actually establish that the brokers are bridging? I am not sure.
Update:
As hardillb pointed out my mistake was that the topic that I was using to publish was not configured to publish out to the other broker. When I updated the conf to topic in_topic out 0 I succeeded in getting the message directly from the test.mosquitto.org broker without including the -h test.mosquitto.org in other words. The message propagated based on the configuration.
The documentation for what I got wrong can be found under the bridging section here: http://mosquitto.org/man/mosquitto-conf-5.html
You won't get anything in terminal A from running the following
mosquitto_pub -t in_topic -m "hello world1"
Because your bridge is set up to only forward things on out_topic from the local broker to the remote (test.mosquitto.org)
topic out_topic out 0
I would change your config if you can:
notifications true
This will publish to $SYS/broker/connections//state - either a 1 or 0 depending on whether the connection is active. This happens on both the remote and the local broker.

Resources