'Can't connect ngx-mqtt client to public IP mosquitto mqtt broker over websockets 9001
Client Side
When I try to connect my angular application running ngx-mqtt to a mqtt broker with a public facing IP address I end up getting these errors on my browser (Firefox) console log:
This is how the connection details are setup on my angular app using ngx-mqtt:
export const MQTT_SERVICE_OPTIONS: IMqttServiceOptions =
{
hostname: '<BROKER IP ADDRESS>',
username: '<BROKER USERNAME>',
password: '<BROKER PASSWORD>',
port: 9001,
// protocol: 'ws',
// path: '/mqtt'
};
They match the details I created on the mqtt broker pc.
Broker/Server Side
On the mqtt broker I'm using mosquitto
I've configured the mosquitto.conf file ensuring websockets is enabled and the correct port 9001 is open as an extra listener alongside the default mqtt listener with port 1883.
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
#include_dir /etc/mosquitto/conf.d
allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883
listener 9001
protocol websockets
After saving the mosquitto.conf file and restarting mosquitto, I run the mosquitto command and get the following output, it only says the default 1883 port is open:
According to this blog running the command mosquitto -c /etc/mosquitto/mosquitto.conf should give me outout that tells me port 9001 is open and listening, and is loaded from the mosquitto.conf file.
But I get no output when I run that command.
I even tried making sure the firewall and port fowarding was enabled for port 9001.
I've also tried doing some of the things suggested in this question. But no matter what I try I can't get websockets to work on this public facing IP mqtt broker.
The strange thing is if I try to do this via a local network mqtt broker it works fine, it's only when I try to connect to a public IP mqtt broker that I get these issues.
If it helps the LAN mqtt broker that works is a raspberry pi 3 running raspberry pi os whereas the public IP WAN mqt broker is running ubuntu on an intel nuc.
If anybody could help it would be appreicated.
Solution 1:[1]
The reason you are not getting the output you are expecting is because mosquitto is already running as a service. When you run just mosquitto it will not use any configuration file, so it will only try and start a listener on port 1883.
You must pass the configuration file on the command line with
mosquitto -c /etc/mosquitto/mosquitto.conf. But this will still fail while the services is still running.
You have 2 choices.
- Stop the service first, then try starting it manually on the command line to test your config file. You can do this with
sudo service mosquitto stop - Just restart the service
sudo service mosquitto restartwhich will cause it to pick up the new config file and should then be listening on port 9001 as well.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


