Search code examples
amazon-ec2websocketmqttmosquitto

Mosquitto 2.0.14 with Websockets


I have a Mosquitto 2.0.14 broker running in a Windows server 2019. I can connect to it over port 8883 using mqtts protocol with SSL enabled. I can also connect to it on port 1883 without SSL.
Now I want to connect using Websockets. So I added a listener as:

listener 9001
protocol websockets
allow_anonymous false
password_file C:\mosquitto\password.txt
http_dir C:\mosquitto\http_dir
set_tcp_nodelay true

I also tried without http_dir and set_tcp_nodelay. I have tried with and without SSL certificates.
I have port 9001 opened in windows firewall as well as on AWS EC2 instance security groups.

This is when I run the mosquitto broker

Port 9001 is being used by mosquitto

But when I try to connect to the broker using ws://broker.mysite.com:9001 or ws://broker.mysite.com:9001/mqtt nothing happens. No error, no connection. Mosquitto does not show any logs even though I can see the request coming in using WireShark. I am really stuck here. please help me out.

Edit:
I have tried to connect using MQTTX, MQTT Explorer and MQTTBox Chrome App.


Solution

  • As per the comments this issue has been noted previously (in this answer), however I figure its worth repeating here to aid others who experience the problem.

    In short - there appears to be a bug in Mosquitto versions 2.0.12, 2.0.13 & 2.0.14 running on Windows when listening for websocket connections. Whilst the broker appears to be listening (on the correct port) it does not, in fact, accept connections (and nothing is logged). I have not investigated in detail but the cause is going to be something about the way the socket is being configured.

    There is a workaround - add socket_domain ipv4 under the listener e.g.

    listener 9001
    protocol websockets
    allow_anonymous false
    password_file C:\mosquitto\password.txt
    http_dir C:\mosquitto\http_dir
    set_tcp_nodelay true
    socket_domain ipv4
    

    See this issue in the github repo for more information.