I tried MQTT Websocket Client to connect to a mosquitto broker/server.
I got the error message:
Connect failed: AMQJS0007E Socket error:undefined.
I have the MQTTX client works with the mosquitto broker/server.
When I compared the GET html packets, I saw the following differences:
From HiveMQ:
Sec-Websocket-Protocol mqttv3.1\r\n
From MQTTX
Sec-Websocket-Protocol mqtt\r\n
How to fix the issue?
How can I configure the HiveMQ to send Sec-Websocket-Protocol mqtt\r\n
Can you connect using MQTT CLI client? The command should look like mqtt publish --topic Test --message Hello --hostname <my-host> --port <my-port> -ws --debug, where the -ws indicates Use WebSocket transport protocol (default: false) and --debug to provide verbose output.
The MQTT CLI client works. It sends: sec-websocket-protocol: mqtt\r\n
(it does not send: sec-websocket-protocol: mqttv3.1\r\n)
I’m using this command from a windows 10 os:
.\mqtt-cli.exe pub --topic “spBv1.0/Sparkplug B Devices/NCMD/C Edge Node 1” --message “Hello” --host “10.1.1.223” --port 61614 -ws --debug
Great that MQTT CLI works! I hope you can use it as a workaround now.
The error that you indicated with WebSocket client I can only reproduce when using a wrong port number (normal MQTT-TCP listener instead of WebSocket one). Hence the question:
Does your broker have many listeners or only WebSocket on the port 61614?
Are you using the same port (61614) with WebSocket client as with MQTT CLI, and in case of the WebSocket Client it fails and in with MQTT CLI it succeeds?
Please feel free to share your WireShark capture port 61614 with WebSocket client and point out where the text “Sec-Websocket-Protocol mqttv3.1\r\n” comes from.
Wireshark data packet, from: .\mqtt-cli.exe pub --topic “spBv1.0/Sparkplug B Devices/NCMD/C Edge Node 1” --message “Hello” --host “10.1.1.223” --port 61614 -ws --debug
(This works). The one from Websockets Client Showcase Does not work.
Frame 4: 276 bytes on wire (2208 bits), 276 bytes captured (2208 bits) on interface \Device\NPF_{B77E048F-733F-45F0-8002-5EF4EF84E778}, id 0
Interface id: 0 (\Device\NPF_{B77E048F-733F-45F0-8002-5EF4EF84E778})
Encapsulation type: Ethernet (1)
Arrival Time: Sep 27, 2022 12:21:54.394893000 Pacific Daylight Time
[Time shift for this packet: 0.000000000 seconds]
Epoch Time: 1664306514.394893000 seconds
[Time delta from previous captured frame: 0.026524000 seconds]
[Time delta from previous displayed frame: 0.026524000 seconds]
[Time since reference or first frame: 0.029651000 seconds]
Frame Number: 4
Frame Length: 276 bytes (2208 bits)
Capture Length: 276 bytes (2208 bits)
[Frame is marked: False]
[Frame is ignored: False]
[Protocols in frame: eth:ethertype:ip:tcp:http]
[Coloring Rule Name: HTTP]
[Coloring Rule String: http || tcp.port == 80 || http2]
Ethernet II, Src: MegaWell_32:6d:05 (10:5b:ad:32:6d:05), Dst: ee:8f:80:24:38:c3 (ee:8f:80:24:38:c3)
Internet Protocol Version 4, Src: 10.1.1.253, Dst: 10.1.1.223
Transmission Control Protocol, Src Port: 63855, Dst Port: 61614, Seq: 1, Ack: 1, Len: 222
Hypertext Transfer Protocol
GET /mqtt HTTP/1.1\r\n
[Expert Info (Chat/Sequence): GET /mqtt HTTP/1.1\r\n]
Request Method: GET
Request URI: /mqtt
Request Version: HTTP/1.1
host: 10.1.1.223:61614\r\n
upgrade: websocket\r\n
connection: upgrade\r\n
sec-websocket-key: EQisDH73AfZ7XdDevbUgyg==\r\n
origin: http://10.1.1.223:61614\r\n
sec-websocket-protocol: mqtt\r\n
sec-websocket-version: 13\r\n
\r\n
[Full request URI: http://10.1.1.223:61614/mqtt]
[HTTP request 1/1]
[Response in frame: 6]
From the HiveMQ MQTT socket client, for testing purpose, could you please replace “Sec-WebSocket-Protocol: mqttv3.1” with “Sec-WebSocket-Protocol: mqtt”? and run a test.
Remove the “v3.1” from “Sec-WebSocket-Protocol: mqttv3.1”.
Find the file that contains the string ( js/mqttws31.js )
Replace the string with what you like and save the file
Start webserver in the project root directory ( the directory where the index.html file is located). For example, I start web server with Python: python3 -m http.server that runs the web app on http://localhost:8000 for me.
If you are interested in my test results: with ‘mqttv3.1’, ‘mqtt’ and ‘mqttv5’ the app works as expected.
Looking forward for you sharing your own test results
I have tested with HiveMQ Cloud Free cluster. To create a free cluster go to https://cloud.hivemq.com/ and sign up – it is free.
If you want me to test with mosquitto, feel free to share your config and exact commands to get that mosquitto running. I will be happy to try to reproduce it for you
Generated the pwfile file with mosquitto_passwd -c pwfile HiveUser1 (used HiveUser1 as username and as password).
Started the hivemq-mqtt-web-client with python3 -m http.server and opened in the browser open http://localhost:8000 – this opens a page with the WebSocket client UI.
The client could successfully connect and subscribe to the mosquitto websocket 9001 with
host: localhost
port: 9001
username:password = HiveUser1:HiveUser1
Everything works as expected.
I hope this helps. I you have further request please let me know.
That is limitation of the MQTT Websocket Client. It only works with hivemq-cloud-cluster-hostname.
This should be highlight so we don’t waste our time for using the MQTT Websocket Client.