socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Error during WebSocket handshake: Unexpected response code: 400

Open shi-yuan opened this issue 11 years ago • 134 comments

Can't find out a solution, I get this error on the browser console: WebSocket connection to 'ws://.../socket.io/?EIO=2&transport=websocket&sid=p3af7ZNfvogtq6tAAAG0' failed: Error during WebSocket handshake: Unexpected response code: 400.

Hava any advice ?

shi-yuan avatar Jan 14 '15 08:01 shi-yuan

I am experiencing the exact same issue at the moment, any help?

arnauddri avatar Jan 14 '15 17:01 arnauddri

I am also having this issue since I installed a SSL cert on my domain.

Here is a better description of the issue: http://stackoverflow.com/questions/28025073/error-during-websocket-handshake-unexpected-response-code-400-with-nginx-proxy

vhf avatar Jan 19 '15 13:01 vhf

Im also having a similar issue connecting with with one of the Android library's. Web sockets won't connect to https through either HAproxy doing ssl termination or letting node do ssl directly. Long polling works fine however

KieranWebber avatar Jan 19 '15 16:01 KieranWebber

I resolve it by changing the domain to the true IP address:

var socket = io.connect('http://182.92.79.215:3007');

shi-yuan avatar Jan 26 '15 10:01 shi-yuan

Had the same issue, my app is behind nginx. Making these changes to my Nginx config removed the error.

location / { proxy_pass http://localhost:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; }

This is originally from https://chrislea.com/2013/02/23/proxying-websockets-with-nginx/

tylercb avatar Mar 17 '15 13:03 tylercb

Same issue here on production server. Development machines do not show the error.

The strange thing is that the connection is working. I can send messages via WebSockets from the server and the client gets them. I can also see the WebSocket connection being established on the server.

I just get this error in developer tools saying:

WebSocket connection to 'wss://.../socket.io/?EIO=3&transport=websocket&sid=2b_v_BXtbwzl5z2yAAAI' failed: Error during WebSocket handshake: Unexpected response code: 400

I'm using Apache ProxyPass to send connections to node.

mikedavies-dev avatar Apr 09 '15 15:04 mikedavies-dev

Same here - full functional but Error message in dev tools. Somewhere else i read its related to the apache version - using 2.2.14 on this machine.

brunsforge avatar Apr 14 '15 10:04 brunsforge

Make sure you're socket.io connection isn't going through an Amazon Load Balancer. Or if so, do this: http://blog.flux7.com/web-apps-websockets-with-aws-elastic-load-balancing

rickyk586 avatar Apr 19 '15 06:04 rickyk586

Same issue here, only in production environment. Websockets seems to work correctly, the application works without problems. But on console log I can see this error.

I'm using Nginx and only one server for node, so it seems to be not a load balancing problem. I was already using the solution suggested by tylercb (with the exception of "proxy_set_header Host $host;") and it is not solving the issue.

napcoder avatar Apr 24 '15 08:04 napcoder

also have problem ,but work well....

zscorpio avatar Apr 28 '15 06:04 zscorpio

I had this same issue. Are you using CloudFlare? Currently, only their Enterprise plan supports WebSockets.

spinda avatar Apr 28 '15 06:04 spinda

Solved for me. It was due to wrong socket.io address in nginx configuration, that was not matching the path using the websocket.

napcoder avatar Apr 28 '15 08:04 napcoder

I googled because I got the same problem and I also use nginx. The solution is to add this part

proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host;

into the nginx configuration file like tylercb mentioned.

rudolfschmidt avatar May 06 '15 22:05 rudolfschmidt

Worked for me. Thanks.

capr avatar May 17 '15 06:05 capr

Got same error directly connecting to my app hosted on Windows Server 2008R2 (no proxy, no IIS). Only dumb intermediate hardware in between.

burmisov avatar May 25 '15 08:05 burmisov

Work fine after switched host name to ip address, i.e. 127.0.0.1:9000. may caused by httpd ProxyPassReverse

ghost avatar Jun 15 '15 07:06 ghost

In my case, the issue was as a result of cloudfare not supporting websockets on the free plan. I turned off CloudFare for the domain and it worked.

nanyaks avatar Aug 09 '15 05:08 nanyaks

I just needed to add some Apache rewrite conditions to handle the websockets, more info here: http://stackoverflow.com/a/27534443/2044993

cpres avatar Sep 30 '15 00:09 cpres

I know this is an old issue, but since it's high on Google search results, but this might help people:

The reason the connection still works even with this error is that socket.io is falling back to AJAX, which is not optimal and you should fix your server configuration.

Btw, this issue should remain closed, it's not a socket.io issue.

alexrosenfeld avatar Dec 16 '15 14:12 alexrosenfeld

@arosenfeld-mentel I keep reading the posts above your comment that "this is not a socket.io issue" but I don't see where anyone says WHAT the issue actually is. I see this myself although, as you say, the connection still seems to work. Any tips would be very gratefully received. Thanks!

cgodkin avatar Dec 17 '15 16:12 cgodkin