IoTian
IoTian

Reputation: 37

Mosquitto MQTT Websocket works with localhost but Not with IP address

I have Mosquitto broker v2.0.14 running on local machine (windows 11). My config file is

listener 1883 
protocol mqtt

listener 9001
protocol websockets
allow_anonymous true

Broker starts

C:\Program Files\mosquitto>mosquitto -v -c mosquitto.conf
1657343153: mosquitto version 2.0.14 starting
1657343153: Config loaded from mosquitto.conf.
1657343153: Opening ipv6 listen socket on port 1883.
1657343153: Opening ipv4 listen socket on port 1883.
1657343153: Opening websockets listen socket on port 9001.
1657343153: mosquitto version 2.0.14 running

My java script code is

var mqtt;
        var recontime = 2000;
        var host = "192.168.0.107";
        var port = 9001;
        function onConnect() {

            console.log("Connected");
            mqtt.subscribe("/Temp");
        
        }

I get error message 'mqttws31.min.js:36 WebSocket connection to 'ws://127.0.0.1:9001/mqtt' failed '

It connects when I replace the IP address with localhost in the java script.

I have tried adding the IP address after listener 9001 but no effect.

I have read through various posts but not getting a clue. Even telnet is not opening the port (when broker is running)

Upvotes: 0

Views: 2962

Answers (2)

user21216174
user21216174

Reputation: 31

I found the answer in github Mosquitto will not bind both IPv4 and IPv6 for Websockets, you need to specify ipv4, configuration: socket_domain ipv4 It is worth noting that I try to configure before the listener, the service fails to start. socket_domain ipv4 must be configured after listener Summary: try to configure socket_domain ipv4 after listener and protocol

listener 1883
listener 8883
protocol websockets
socket_domain ipv4

Upvotes: 3

IoTian
IoTian

Reputation: 37

Strange but it worked. I was using ver 2.0.14. When I down versioned to 2.0.9a, it started connecting

Upvotes: 0

Related Questions