paho.mqtt.javascript icon indicating copy to clipboard operation
paho.mqtt.javascript copied to clipboard

Connection closed before receiving a handshake response

Open pokal4u opened this issue 7 years ago • 22 comments
trafficstars

Hi, We are getting below error in some browsers(chrome or mozilla) when we connect mqtt using web sockets. When we clear browser history then connected. Error: WebSocket connection to 'wss://host:port/mqtt' failed: Connection closed before receiving a handshake response Paho.MQTT.ClientImpl._doConnect @ mqttws31.js:979

My connection:

var client = new Paho.MQTT.Client("host", Number(port), "myClientId" + new Date().getTime()); client.connect({userName:username,password:password,onSuccess:onConnect,onFailure:doFail,useSSL:true,cleanSession:false});

So please look into this. Thanks

pokal4u avatar May 04 '18 08:05 pokal4u

any one please look into this

pokal4u avatar May 08 '18 05:05 pokal4u

Maybe you don't boot the broker properly. Which broker do you use? Or you can try set useSSL=false.

DayTimeDreamer avatar May 08 '18 05:05 DayTimeDreamer

broker: mosquitto. Our site running on https, so useSSL should be true. If set false getingt error. And mqtt connected for some users(in all browsers) without clearing browser cache.

Please check attache files of chrome versions and response code of different systems, one system got error and another one connected succesfully.

Mosquitto configuration:

Plain WebSockets configuration listener port protocol websockets

WebSockets over TLS/SSL listener port protocol websockets cafile ca.crt certfile cer.crt keyfile key

mqtt_connet mqtt_connet1 mqtt_disconnect mqtt_disconnect1

pokal4u avatar May 08 '18 05:05 pokal4u

This looks like it could be an HTTPS issue, does this only happen with Secure websockets connection, or with plain connections as well?

jpwsutton avatar May 08 '18 10:05 jpwsutton

for plain connection also getting same error in same browser, but why this relation with browser cache? when I clear cache then connected

pokal4u avatar May 08 '18 11:05 pokal4u

Can you try this against the server at iot.eclipse.org and confirm if you see the same problems connecting to that please?

jpwsutton avatar May 08 '18 12:05 jpwsutton

using this "iot.eclipse.org" connected in all browsers. Please suggest me where I did wrong. I will share more info if required.

pokal4u avatar May 08 '18 12:05 pokal4u

Looks like it might be a configuration issue with your instance of Mosquitto, I'd suggest checking the Mosquitto logs to see if there is anything in that might explain the connection loss.

jpwsutton avatar May 08 '18 12:05 jpwsutton

Where can I enable logs and any default location for saved logs?

pokal4u avatar May 08 '18 12:05 pokal4u

in logs getting this error "Ran out of header data space"

pokal4u avatar May 09 '18 04:05 pokal4u

Hi @pokal4u were you able to resolve this? I am having the same issue!

luiscolmenares avatar Jul 19 '18 16:07 luiscolmenares

Hi. Me too

l0stmyself avatar Feb 09 '19 15:02 l0stmyself

for plain connection also getting same error in same browser, but why this relation with browser cache? when I clear cache then connected

It's connected perfectly when I clear browser cache

jabedhasan21 avatar Feb 12 '19 15:02 jabedhasan21

tagging onto this as well. when i connect in an incognito window, no issues. but from my normal session i am not completing the handshake.

zcmack avatar Mar 17 '19 22:03 zcmack

have you solved this issue? I am having the same issue! Please someone help me@pokal4u

jayanthiNNaik avatar Oct 09 '19 10:10 jayanthiNNaik

yes im facing same problem ,when i connect in an incognito window, no issues. but from my normal session i am not completing the handshake. can you suggest how to solve this problem. @luiscolmenares, @DayTimeDreamer, @pokal4u.

kumar1008k avatar Oct 09 '19 10:10 kumar1008k

Anyone resolved this? Any solution?

scovic avatar Oct 22 '19 13:10 scovic

Have some problem! Is there any solution?

u236 avatar Oct 30 '19 13:10 u236

One way to debug the issue would be to add the below line into your /etc/mosquitto/mosquitto.conf websockets_log_level 255 and restart the mosquitto process.

You can then tail the generated log file and see what is filling up the header.

Hope that helps.

argupta23 avatar Dec 12 '19 05:12 argupta23

I also have this problem.

Related to

  • https://github.com/mqttjs/MQTT.js/issues/976
  • https://github.com/mqttjs/MQTT.js/issues/878

In browser console I see Connection closed before receiving a handshake response when trying to connect. Cognito mode or clear cache both allow the connection to work.

Tried websockets_log_level 255 and I am also seeing "Ran out of header data space" in the logs. (logs included below) I can see a bunch of WSI_TOK logs which include recognisable sequences for language and cookie It looks like the cookie data is the last thing in the logs before "Ran out of header data space"

If I use the browser settings to block cookies for the site, the mqtt connection works but other features like login break.

https://github.com/eclipse/mosquitto/issues/208 suggests using websockets_headers_size 4096 in mosquitto.conf (issue suggests max_http_header_data but mosquitto_conf reference says websockets_headers_size) works for versions >= 1.6 of mosquitto (which probably means build mosquitto from source) This works for me.

Another approach is ensuring that the web server does not set the cookies. In my case Flask is setting a session cookie. I haven't tried this option but suspect that might work too.

cheers Steve

============================================== 1585821012: known hdr 22 1585821012: WSI_TOK_(22) ' ' 1585821012: WSI_TOK_(22) 'e' 1585821012: WSI_TOK_(22) 'n' 1585821012: WSI_TOK_(22) '-' 1585821012: WSI_TOK_(22) 'U' 1585821012: WSI_TOK_(22) 'S' 1585821012: WSI_TOK_(22) ',' 1585821012: WSI_TOK_(22) 'e' 1585821012: WSI_TOK_(22) 'n' 1585821012: WSI_TOK_(22) ';' ...................................... 1585821012: WSI_TOK_(22) '6' '585821012: WSI_TOK_(22) ' 1585821012: * 1585821012: WSI_TOKEN_SKIPPING_SAW_CR ' ' 1585821012: WSI_TOKEN_NAME_PART 'C' (mode=0) 1585821012: WSI_TOKEN_NAME_PART 'o' (mode=0) 1585821012: WSI_TOKEN_NAME_PART 'o' (mode=0) 1585821012: WSI_TOKEN_NAME_PART 'k' (mode=0) 1585821012: WSI_TOKEN_NAME_PART 'i' (mode=0) 1585821012: WSI_TOKEN_NAME_PART 'e' (mode=0) 1585821012: WSI_TOKEN_NAME_PART ':' (mode=0) 1585821012: known hdr 26 1585821012: WSI_TOK_(26) ' ' 1585821012: WSI_TOK_(26) 's' 1585821012: WSI_TOK_(26) 'e' 1585821012: WSI_TOK_(26) 's' 1585821012: WSI_TOK_(26) 's' 1585821012: WSI_TOK_(26) 'i' 1585821012: WSI_TOK_(26) 'o' 1585821012: WSI_TOK_(26) 'n' 1585821012: WSI_TOK_(26) '=' 1585821012: WSI_TOK_(26) '.' 1585821012: WSI_TOK_(26) 'e' 1585821012: WSI_TOK_(26) 'J' 1585821012: WSI_TOK_(26) 'y' 1585821012: WSI_TOK_(26) '9' ............................................................................................................................... 1585821012: WSI_TOK_(26) 's' 1585821012: WSI_TOK_(26) 'z' 1585821012: WSI_TOK_(26) 'I' 1585821012: WSI_TOK_(26) 'v' 1585821012: Ran out of header data space 1585821012: lws_parse failed 1585821012: lws_header_table_detach: wsi 0x564413a70440: ah 0x564413a56e70 (tsi=0, count = 1) 1585821012: closing connection at lws_read bail: 1585821012: lws_close_free_wsi: shutting down connection: 0x564413a70440 (sock 12, state 2)

syntithenai avatar Apr 02 '20 22:04 syntithenai

I had the same issue. Interestingly only with Chrome. MQTT Box and Safari work well. It's not a real fix but replacing the DNS name with the IP of the mqtt mosquitto host worked for me. Still looking for the real problem. Does not seem to be a paho mqtt issue.

snoopyCode avatar Apr 13 '20 19:04 snoopyCode

@snoopyCode Thank you so much for suggesting that. Been struggling trying to fix this annoying issue, and it turns out my host needed to be an IP and not a hostname.

mortenmoulder avatar Jul 17 '20 00:07 mortenmoulder