# Establishing a websockets-MQTT connection through Node-Red

**URL:** <https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072>\
**Category:** General\
**Created:** [15 September 2021 17:57 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072 "2021-09-15T17:57:08Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohamed](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@mohamed](https://discourse.nodered.org/u/mohamed)\
**Post date:** [15 September 2021 17:57 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/1 "2021-09-15T17:57:08Z")

</div>

Hello everyone ![☺️](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/5353a92024b15cda48b6f34f9b2ed185697d92e6.png). 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](https://github.com/geeks-r-us/mqtt-panel)

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 ?!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 September 2021 18:56 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/2 "2021-09-15T18:56:17Z")

</div>

> [@mohamed](#):
>
> 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.

> [@mohamed](#):
>
> allows grafana to communicate with an mqtt server. I have installed the plugin but i can't connect it to the broker!

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.

---

<div class="post-metadata">

**Author:** ![mohamed](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@mohamed](https://discourse.nodered.org/u/mohamed)\
**Post date:** [15 September 2021 19:24 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/3 "2021-09-15T19:24:45Z")

</div>

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:

```auto
# 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:  
 ![123](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/08356930603feb13933957f2c5df8bc0bcd62b82.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 September 2021 19:53 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/4 "2021-09-15T19:53:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mohamed](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@mohamed](https://discourse.nodered.org/u/mohamed)\
**Post date:** [15 September 2021 19:54 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/5 "2021-09-15T19:54:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 September 2021 19:55 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/6 "2021-09-15T19:55:07Z")

</div>

In that case it is a grafana issue.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 September 2021 19:55 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/7 "2021-09-15T19:55:47Z")

</div>

Or network connectivity/firewall between browser and broker?

---

<div class="post-metadata">

**Author:** ![mohamed](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@mohamed](https://discourse.nodered.org/u/mohamed)\
**Post date:** [15 September 2021 19:57 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/8 "2021-09-15T19:57:32Z")

</div>

if that, where i can go ?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 September 2021 20:03 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/9 "2021-09-15T20:03:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mohamed](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@mohamed](https://discourse.nodered.org/u/mohamed)\
**Post date:** [15 September 2021 20:08 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/10 "2021-09-15T20:08:00Z")

</div>

![88](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/8/f8f01f6fb69cfb5e65d5343c7ae40f57329ede0b.png)

that's it

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 September 2021 20:11 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/11 "2021-09-15T20:11:31Z")

</div>

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

Show us the MQTT explorer connection config that works.

---

<div class="post-metadata">

**Author:** ![mohamed](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@mohamed](https://discourse.nodered.org/u/mohamed)\
**Post date:** [15 September 2021 20:16 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/12 "2021-09-15T20:16:06Z")

</div>

> [@Colin](#):
>
> Is that in a browser on the same PC that you ran MQTT Explorer on?

yes, it is on the same PC.

> [@Colin](#):
>
> Show us the MQTT explorer connection config that works

 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/2/528c1f85131e436f7216b1b8772163cfad1da9f1.png)  
 ![2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/7/1778abab658ac4fe01dbcce8d15e49cfa1cf4df3.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 September 2021 20:24 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/13 "2021-09-15T20:24:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mohamed](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@mohamed](https://discourse.nodered.org/u/mohamed)\
**Post date:** [15 September 2021 20:33 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/14 "2021-09-15T20:33:15Z")

</div>

> [@Colin](#):
>
> 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

 ![5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/22a8b6440f807efde64d1191386aedd3d626d3c0.png)

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

 ![6](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/476545bc57915e373dec6f5b477e7b2e749dae48.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 September 2021 20:35 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/15 "2021-09-15T20:35:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mohamed](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@mohamed](https://discourse.nodered.org/u/mohamed)\
**Post date:** [15 September 2021 20:39 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/16 "2021-09-15T20:39:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 November 2021 20:40 UTC](https://discourse.nodered.org/t/establishing-a-websockets-mqtt-connection-through-node-red/51072/17 "2021-11-14T20:40:14Z")

</div>

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