# Connect failed: AMQJS0007E Socket error:undefined

**URL:** <https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465>\
**Category:** HiveMQ Client Library\
**Created:** [September 24, 2022, 12:00am UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465 "2022-09-24T00:00:58Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![dennisnguy](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dennisnguy](https://community.hivemq.com/u/dennisnguy)\
**Post date:** [September 24, 2022, 12:00am UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/1 "2022-09-24T00:00:58Z")

</div>

I tried [MQTT Websocket Client](http://www.hivemq.com/demos/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

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [September 26, 2022, 3:08pm UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/2 "2022-09-26T15:08:39Z")

</div>

Hi @dennisnguy ,

Can you connect using [MQTT CLI](https://hivemq.github.io/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

---

<div class="post-metadata">

**Author:** ![dennisnguy](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dennisnguy](https://community.hivemq.com/u/dennisnguy)\
**Post date:** [September 26, 2022, 4:18pm UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/3 "2022-09-26T16:18:00Z")

</div>

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](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://10.1.1.223:61614/mqtt)]  
[HTTP request 1/1]  
[Response in frame: 8]

Thanks

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [September 27, 2022, 8:58am UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/4 "2022-09-27T08:58:42Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![dennisnguy](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dennisnguy](https://community.hivemq.com/u/dennisnguy)\
**Post date:** [September 27, 2022, 7:27pm UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/5 "2022-09-27T19:27:21Z")

</div>

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](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://10.1.1.223:61614/mqtt)]  
[HTTP request 1/1]

---

<div class="post-metadata">

**Author:** ![dennisnguy](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dennisnguy](https://community.hivemq.com/u/dennisnguy)\
**Post date:** [September 27, 2022, 8:13pm UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/6 "2022-09-27T20:13:21Z")

</div>

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.

```auto
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]

```

---

<div class="post-metadata">

**Author:** ![dennisnguy](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dennisnguy](https://community.hivemq.com/u/dennisnguy)\
**Post date:** [October 6, 2022, 3:45pm UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/7 "2022-10-06T15:45:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [October 7, 2022, 8:01am UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/8 "2022-10-07T08:01:17Z")

</div>

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.](https://github.com/hivemq/hivemq-mqtt-web-client)
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](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

---

<div class="post-metadata">

**Author:** ![dennisnguy](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dennisnguy](https://community.hivemq.com/u/dennisnguy)\
**Post date:** [October 7, 2022, 1:20pm UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/9 "2022-10-07T13:20:35Z")

</div>

Hi Dasha,

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

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [October 7, 2022, 1:46pm UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/10 "2022-10-07T13:46:52Z")

</div>

@dennisnguy ,

I have tested with HiveMQ Cloud Free cluster. To create a free cluster go to [https://cloud.hivemq.com/](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 😉

Regards,  
Dasha

---

<div class="post-metadata">

**Author:** ![dennisnguy](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dennisnguy](https://community.hivemq.com/u/dennisnguy)\
**Post date:** [October 7, 2022, 2:41pm UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/11 "2022-10-07T14:41:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [October 7, 2022, 2:56pm UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/12 "2022-10-07T14:56:38Z")

</div>

You forgot to share the steps with me 🙂

---

<div class="post-metadata">

**Author:** ![dennisnguy](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dennisnguy](https://community.hivemq.com/u/dennisnguy)\
**Post date:** [October 7, 2022, 4:49pm UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/13 "2022-10-07T16:49:59Z")

</div>

Hi Dasha,

Below are basic steps to setup the mosquitto broker:

1. Download the mosquitto broker/server at: [Download | Eclipse Mosquitto](https://mosquitto.org/download/)
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

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [October 11, 2022, 12:32pm UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/14 "2022-10-11T12:32:03Z")

</div>

Thanks, @dennisnguy ,

Started a mosquitto server with the conf:

```auto

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

---

<div class="post-metadata">

**Author:** ![dennisnguy](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dennisnguy](https://community.hivemq.com/u/dennisnguy)\
**Post date:** [October 11, 2022, 2:13pm UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/15 "2022-10-11T14:13:53Z")

</div>

Hi Dasha,

Could you please test the mosquitto broker with: [MQTT Websocket Client](http://www.hivemq.com/demos/websocket-client/)

Thanks,  
Dennis

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [October 11, 2022, 2:55pm UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/16 "2022-10-11T14:55:21Z")

</div>

@dennisnguy ,

Now I understand where the confusion is coming from. This [MQTT Websocket Client](http://www.hivemq.com/demos/websocket-client/) only works with hivemq-cloud-cluster-hostname. The same is documented in the [Getting started with HiveMQ Websocket Client](https://console.hivemq.cloud/clients/websocket-web) article.

If you want the Websocket client for your own host, you should run it yourself, as I [suggested earlier in this thread](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/8).

I hope this helps

Kind regards,  
Dasha from HiveMQ team

---

<div class="post-metadata">

**Author:** ![dennisnguy](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dennisnguy](https://community.hivemq.com/u/dennisnguy)\
**Post date:** [October 14, 2022, 3:49am UTC](https://community.hivemq.com/t/connect-failed-amqjs0007e-socket-error-undefined/1465/17 "2022-10-14T03:49:57Z")

</div>

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
