Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket connection to 'wss://' Error during WebSocket handshake: Unexpected response code: 400

I use socket.io in my node.js app that's running on express.

Everything words fine on the local version (localhost) however when I switch to my production server (which is served via https using a custom certificate), I get the following error in my browser console:

websocket.js:112 WebSocket connection to 'wss://infranodus.com/socket.io/?EIO=3&transport=websocket&sid=j_WBxkPY_RlpF9_ZAANP' failed: Error during WebSocket handshake: Unexpected response code: 400

I made a research (issue referenced here) and it turns out this happens because my app / hosting provider blocks connections like wss and my socket.io falls back on AJAX to make requests (which functions OK, but sometimes there are bugs).

So I wanted to ask you if I could do modifications to my app to get rid of this error?

Just FYI currently all requests to http://infranodus.com are forwarded (via static .htaccess) to https://infranodus.com and my app.js file (the part of the server activation looks like that):

var http = require('http');

var app = express();

var server = http.Server(app);
var io = require('socket.io')(server);

app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

and the way I require sockets in my front-end file:

<script src="/socket.io/socket.io.js"></script>

and then

var socket = io();

Maybe the problem is that I activate server in my node.js app using http and not https? But I would not like to switch to that because I don't know where my certificates are stored and I would not like to change the backend code too much.

UPDATE (21/10/2018): We figured out the problem is with nginx server and due to the limitations of some hosting providers who do not allow users to edit nginx servers, websockets over secure protocol get blocked or get 400 error. It would be nice to resolve this in sockets.io as it's a problem that many users have. Any ideas?

TO REPRODUCE THE ISSUE: Please, go open your Javascript Console and go to https://infranodus.com/news/english

SOURCE CODE: https://github.com/noduslabs/infranodus

like image 271
Aerodynamika Avatar asked Mar 30 '18 13:03

Aerodynamika


People also ask

How do I fix WebSocket connection error?

Solution 1Check that all the Bot Insight services are running. Check that your firewall settings are configured to accept incoming websocket data. Try to use a different web browser. Restart the Bot Insight Visualization and Bot Insight Scheduler services.

What is WebSocket WSS?

The WebSocket protocol specification defines ws (WebSocket) and wss (WebSocket Secure) as two new uniform resource identifier (URI) schemes that are used for unencrypted and encrypted connections respectively.

What is WebSocket handshake?

WebSockets - Overview In computer science, handshaking is a process that ensures the server is in sync with its clients. Handshaking is the basic concept of Web Socket protocol. The following diagram shows the server handshake with various clients −


Video Answer


2 Answers

I solved it with this:

var socket = io.connect("http://localhost:3000", {
   forceNew: true,
   transports: ["polling"],
});
like image 72
Paris López Avatar answered Oct 02 '22 00:10

Paris López


Check if you using express-status-monitor as a middleware on express, this makes http call on the first (request) handshake of WebSocket goes failed, if not maybe another factor like proxy (nginx) or similar like that

Look here More details about this error

like image 25
Aditya Kresna Permana Avatar answered Oct 02 '22 00:10

Aditya Kresna Permana