Establishing a websockets-MQTT connection through Node-Red

Hello everyone ☺️. I need to use mosquitto mqtt over websockets. I'm using a raspberry pi as a host server for several tools such as mosquitto broker,node red, influx db, and grafana as a data logging system. I'm trying to use grafana to control a sonoff switch through mqtt. I found this plugin: GitHub - geeks-r-us/mqtt-panel: Simple Grafana panel that allows to communicate with a mqtt server

that allows grafana to communicate with an mqtt server. I have installed the plugin but i can't connect it to the broker! In the plugin's GitHub page the author have mentioned a limitation says " Due to the MQTT client runs in the browser it can only establish mqtt connection over websockets and can only connect to servers reachable by your browser."

could Nde-Red be used to establish a connection between mosquitto mqtt broker and a client over websockets ?!

That's not really how MQTT works.

This isn't really a node-red question - however...

  • have you enabled websockets in your broker?
  • what version of mosquitto are you using? Older versions (especially windows binaries) did not have websockets capabilities

The first test would be to use something like MQTT explorer to see if you can actually connect over websockets.

thanks for your reply. I'm using mosquitto version 1.5.7 . I tried to enable webosckets by adding these lines to the mosquitto.config file:

# Your default port.
listener 1883
protocol mqtt
require_certificate false

# Web-socket for the Grafana app.
listener 9001
protocol websockets

I saved the new configurations and restarted mosquitto, but I obtained that only 1883 port is working as shown:

In that screenshot you have restarted the mosquitto service and then started another instance of mosquitto in the terminal. That is why it says the address is in use (by the mosquitto service you just restarted). Instead open two terminals. in one run
sudo tail -f /var/log/mosquitto/mosquitto.log
and leave it running. In the other restart the service
sudo systemctl restart mosquitto
That is a better way to restart it than using the obsolete init.d command. You should see the log of the restart in the first terminal.

The first test would be to use something like MQTT explorer to see if you can actually connect over websockets

i tested the broker using MQTT explorer , used protocol ws with port 9001.
it worked and i cuold publish to my switch topic.

In that case it is a grafana issue.

Or network connectivity/firewall between browser and broker?

if that, where i can go ?

Show us a screenshot of the plugin configuration you have entered.

1 Like

that's it

Is that in a browser on the same PC that you ran MQTT Explorer on?

Show us the MQTT explorer connection config that works.

yes, it is on the same PC.

That all looks ok. Watch the mosquitto log as I indicated in my earlier post and see if there are any clues there.

Is it necessary to click Apply in Grafana to get it to use the settings?

It seems that this is the problem, but in the opposite way. When I try from inside the settings page it works fine, but when I try from outside it doesn't work.

i tested the switch here and it worked well

but when i test here it doesn't work !!!!

Probably best to ask on the plugin Issues page. Looking at other issues raised it seems it is well supported.

1 Like

Ok, I'll try there. I'm so grateful for your help.