Connect failed: AMQJS0007E Socket error:undefined

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

Hi @dennisnguy ,

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.

I hope this helps,
Dasha from HiveMQ team

Hi @Daria_H

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

D:\work\tools\mqtt_cli_hivemq>.\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
Client ‘UNKNOWN@10.1.1.223’ sending CONNECT MqttConnect{keepAlive=60, cleanStart=true, sessionExpiryInterval=0}
Client ‘UNKNOWN@10.1.1.223’ received CONNACK MqttConnAck{reasonCode=SUCCESS, sessionPresent=false, assignedClientIdentifier=auto-551A30BA-F241-90B6-8B86-83333C476ED6, restrictions=MqttConnAckRestrictions{receiveMaximum=65535, maximumPacketSize=268435460, topicAliasMaximum=10, maximumQos=EXACTLY_ONCE, retainAvailable=true, wildcardSubscriptionAvailable=true, sharedSubscriptionAvailable=true, subscriptionIdentifiersAvailable=true}}
Client ‘auto-551A30BA-F241-90B6-8B86-83333C476ED6@10.1.1.223’ sending PUBLISH (‘Hello’) MqttPublish{topic=spBv1.0/Sparkplug B Devices/NCMD/C Edge Node 1, payload=5byte, qos=AT_MOST_ONCE, retain=false}
Client ‘auto-551A30BA-F241-90B6-8B86-83333C476ED6@10.1.1.223’ received PUBLISH acknowledgement MqttPublishResult{publish=MqttPublish{topic=spBv1.0/Sparkplug B Devices/NCMD/C Edge Node 1, payload=5byte, qos=AT_MOST_ONCE, retain=false}}

Wireshark info:
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: rEDydrkMPRMBTGyP02ol3Q==\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: 8]

Thanks

@dennisnguy ,

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.

Kind regards,
Dasha

Hi Dasha,

  • My mosquitto broker has only one mqtt websockets port number = 61614
  • I have used the same port number when I tested mqtt websockets clients.

Below is the mqtt websockets captured from Wireshark using the web websocket-client
Frame 314874: 581 bytes on wire (4648 bits), 581 bytes captured (4648 bits) on interface \Device\NPF_{B77E048F-733F-45F0-8002-5EF4EF84E778}, id 0
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: 57979, Dst Port: 61614, Seq: 1, Ack: 1, Len: 527
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
Connection: Upgrade\r\n
Pragma: no-cache\r\n
Cache-Control: no-cache\r\n
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36\r\n
Upgrade: websocket\r\n
Origin: http://www.hivemq.com\r\n
Sec-WebSocket-Version: 13\r\n
Accept-Encoding: gzip, deflate\r\n
Accept-Language: en-US,en;q=0.9\r\n
Sec-WebSocket-Key: Mlv7c4rOVfriQYFdRZ7Dgw==\r\n
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n
Sec-WebSocket-Protocol: mqttv3.1\r\n
\r\n
[Full request URI: http://10.1.1.223:61614/mqtt]
[HTTP request 1/1]

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]

Hi Dasha,

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

Thanks,
Dennis

Hey @dennisnguy ,

Sure, you can do it the following way:

  1. Clone the repo GitHub - hivemq/hivemq-mqtt-web-client: A websockets based MQTT Client for your browser.
  2. Find the file that contains the string ( js/mqttws31.js )
  3. Replace the string with what you like and save the file
  4. 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

Dasha

Hi Dasha,

Which mqtt websockets broker/server did you test the client with?
I tested with mosquitto broker/server.
Thanks,
Dennis

@dennisnguy ,

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 :wink:

Regards,
Dasha

Please test with mosquito broker/server.
Just set the port number and protocol to websockets.
Thanks,
Dennis

You forgot to share the steps with me :slight_smile:

Hi Dasha,

Below are basic steps to setup the mosquitto broker:

  1. Download the mosquitto broker/server at: Download | Eclipse Mosquitto
  2. Depending on you machine OS, you can download and install windows or linux version.
    Edit the mosquitto.conf

Port to use for the default listener.

port 9001

Choose the protocol to use when listening.

This can be either mqtt or websockets.

Websockets support is currently disabled by default at compile time.

Certificate based TLS may be used with websockets, except that

only the cafile, certfile, keyfile and ciphers options are supported.

protocol websockets
3. Start the mosquitto broker/server

Hope this help,
Thanks,|
Dennis

Thanks, @dennisnguy ,

Started a mosquitto server with the conf:


password_file pwfile

listener 1883
protocol mqtt

# Websockets
listener 9001
protocol websockets

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.

Kind regards,
Dasha from HiveMQ team

Hi Dasha,

Could you please test the mosquitto broker with: MQTT Websocket Client

Thanks,
Dennis

@dennisnguy ,

Now I understand where the confusion is coming from. This MQTT Websocket Client only works with hivemq-cloud-cluster-hostname. The same is documented in the Getting started with HiveMQ Websocket Client article.

If you want the Websocket client for your own host, you should run it yourself, as I suggested earlier in this thread.

I hope this helps

Kind regards,
Dasha from HiveMQ team

Hi Dasha,

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.

Thanks for your help.
Dennis