Ahmed Al-haddad
Ahmed Al-haddad

Reputation: 833

How to connect to a Mosquitto broker on a Raspberry Pi through web sockets?

I am trying to connect to a Raspberry Pi that has the Mosquitto broker installed. The client on the RPi is connected using:

client.connect("127.0.0.1", 1883, 60)

I tried to connect to it on my MQTT JavaScript client using the following specifications but I failed:

client = new Paho.MQTT.Client("10.101.125.190", 1883,"myclientid_" + parseInt(Math.random() * 100, 10)); 

I also tried changing the port to 8080 from the JavaScript side but I still failed. If I change the port to 8080 on the RPi then it won't even connect.

This is the error that I am getting at the moment:

WebSocket connection to 'ws://10.101.125.190:1883/mqtt' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET

So, what do I need to change to fix this error? The RPi and my JS client are both in the same local network.

Edit: I forgot to mention that I have already tried this with test.mosquitto.org - 8080 and it worked, but as soon as I change the address then I start getting the error.

Upvotes: 0

Views: 1997

Answers (1)

hardillb
hardillb

Reputation: 59608

MQTT over websockets does not share the same port as native MQTT.

You will need to add a new listener to your mosquitto config.

You will need to add something like the following to the end of your /etc/mosquitto/mosquitto.conf (or in a seperate file in /etc/mosquitto/mosquitto.d)

listener 1884
protocol websockets

Then need to update your JavaScript to connect to port 1884 not 1883

You will also need to be using a version of mosquitto newer than than 1.4.x iirc the default version that is packaged for raspbian is too old. Follow the instructions here to get a newer version.

Upvotes: 2

Related Questions