-1

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:

enter image description here

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:

enter image description here

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.

SneakyShrike
  • 723
  • 1
  • 10
  • 31
  • 1
    Please don not post images of text, always post the actual text and then use the toolbar to format it. Images are impossible to search, often hard to read and of no use to users that need to use screen readers. – hardillb Jan 24 '22 at 16:13

2 Answers2

0

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.

  1. 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
  2. Just restart the service sudo service mosquitto restart which will cause it to pick up the new config file and should then be listening on port 9001 as well.
hardillb
  • 54,545
  • 11
  • 67
  • 105
  • Thanks, I'll try looking at it tomorrow when I'm back at that computer. I'll let you know the outcome and I'll clean up my question as well. I can't do it right now. – SneakyShrike Jan 24 '22 at 17:14
  • Okay I tried doing the command mosquitto -c /etc/mosquitto/mosquitto.conf, with the mosquitto service stopped but it just tells me: 1643186898: Error: Unable to open log file /var/log/mosquitto/mosquitto.log for writing. If I run the command with sudo it just hangs forever. – SneakyShrike Jan 26 '22 at 08:49
  • Have you looked at the mosquitto.log file since restarting it? – hardillb Jan 26 '22 at 09:03
  • I tried giving myself write permissions on the /var/log/mosquitto.log file, and I noticed I had 7 other log files that were zipped, so i deleted all of the log files and created a fresh one. I can run the -c command without sudo now but it just hangs. – SneakyShrike Jan 26 '22 at 09:05
  • No it doesn't, it's running and logging to the file. Look in the file (in a separate terminal) – hardillb Jan 26 '22 at 09:08
  • I tailed the mosquitto.log file but the only output is: 1643187756: mosquitto version 2.0.14 starting, I can't see anything about listening ports. – SneakyShrike Jan 26 '22 at 09:11
0

Okay so it turns out there was nothing wrong with my configuration. The issue was Firefox for some reason, I tried running my angular app with Chromium and it connected to the MQTT broker right away.

SneakyShrike
  • 723
  • 1
  • 10
  • 31