ws: wss: Error during WebSocket handshake: Unexpected response code: 200

Salutations,

I’m running a WebSocket server using this module, but am having issues connecting with certain browsers. The server is served through ssl, so I connect to it using wss.

The Issue

When I connect to the website using Google Chrome, everything works as intended. However, when I try to connect using Chromium, I get the following error:

WebSocket connection to 'wss://domain.name/' failed: Error during WebSocket handshake: Unexpected response code: 200

It should be noted that the client connects perfectly fine on my test instance, connecting to ws://localhost:8085. This seems to be an issue only with wss protocols. Obviously, using ws protocols over an ssl connection is not a viable option.

I have tried directly connecting via IP and port, but get the following error:

WebSocket connection to 'wss://ip.addr.he.re:8190/' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED

My Code

Server:

const WebSocketServer = require('ws').Server;
const wss = new WebSocketServer({
    server: server // express server object, running on port 8085
  //port: 8190 // used when testing direct connections
});

Browser:

var wss = new WebSocket("wss://domain.name");

Sidenotes

I suspect this issue is due to chromium’s websocket support itself. I noticed that there is client support available. Is there a browserified version of this module available for me to load via a <script> tag, or another means to use this module in a browser?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 4
  • Comments: 32 (4 by maintainers)

Most upvoted comments

A fix for me was setting these response headers: Host, Connection and Upgrade. For nginx case (socket.io server is behind the proxy):

location /foo/ {
    proxy_pass http://foobar:3005/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
}

This is strange. Did you try with other browsers? Does it work with Firefox? The strange thing is that you get a 200 status code. It seems that you are not hitting the WebSocket server as it will never answer with 200. Is there a proxy in the middle?

A fix for me was setting these response headers: Host, Connection and Upgrade. For nginx case (socket.io server is behind the proxy):

location /foo/ {
    proxy_pass http://foobar:3005/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
}

A hero can be anyone, even a man doing something as simple as putting a working nginx config in Github issues.

nginx don’t support websocket,but it can proxy websocket connetions,you neet config it: location / { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection “upgrade”; }

Reference link: http://nginx.org/en/docs/http/websocket.html

Is there any fix for those who are hosting on Heroku and not Nginx?

proxy_set_header Host $host;

this saved my life tonight. Thank you!

A fix for me was setting these response headers: Host, Connection and Upgrade. For nginx case (socket.io server is behind the proxy):

location /foo/ {
    proxy_pass http://foobar:3005/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
}

I think this was a general web socket issue, i experienced the same error while working with SignalrR for an asp.net core site hosted on linux.

I changed nginx config file specifying ‘proxy_set_header Connection “upgrade”’ and everything worked.

Thank you for your suggestion sir!