Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io - failed: Connection closed before receiving a handshake response

Tags:

Socket.io for NodeJS doesn't seem to work as a websocket server

For some reason, socket.io ALWAYS fallback to the long polling and if I force the websocket transport layer, it will error out:

failed: Connection closed before receiving a handshake response

The right ports are open. I'm using the chat example from socket.io. I set up the server on http://jmi.io:3000. As you can see, it works fine with a beautiful long polling channel but now try the websocket connection from a websocket client and...

WebSocket connection to 'ws://jmi:3000/' failed: Connection closed before receiving a handshake response

I have only one node single threaded and have the exact same package.json than in the chat example repo.

{
  "name": "socket-chat-example",
  "version": "0.0.1",
  "description": "my first socket.io app",
  "dependencies": {
    "express": "4.3.1",
    "socket.io": "1.2.0"
  }
}

Thank you for your help

like image 737
Jean Meyer Avatar asked Oct 31 '14 00:10

Jean Meyer


People also ask

How do I resolve a 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.

Which event happens when there is an error in socket communication?

The error event is fired when a connection with a WebSocket has been closed due to an error (some data couldn't be sent for example).

What causes a WebSocket to fail?

The most common cause of Websocket error is when you connect to DSS through a proxy. Websockets is a fairly recent protocol and many enterprise proxies do not support it. The websocket connection will not establish and you will see this message.


2 Answers

I had the exact same issue because I was defining 'io' twice. Double check where you are defining io in your code and ensure you are not defining the variable io twice.

Example of what I was doing wrong:

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

var io = require('socket.io').listen(server.listen(config.port, config.ip, function () {
    console.log('Express server listening on %d, in %s mode', config.port,     
    app.get('env'));
}));

Example of what fixed the issue:

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

server.listen(config.port, config.ip, function () {
    console.log('Express server listening on %d, in %s mode', config.port, 
    app.get('env'));
});
like image 179
jetset Avatar answered Oct 17 '22 03:10

jetset


Slightly related to what @Lucas Klaassen answered: I had the following:

let express = require('express');
let app = express();
let http = require('http').Server(app);
let io = require('socket.io')(http);
// this is the culprit:
app.listen(port);

Changing last line is what fixed it:

http.listen(port);
like image 25
Andy Avatar answered Oct 17 '22 03:10

Andy