Socket.io: WebSocket is closed before the connection is established.

Created on 20 Mar 2012  Â·  33Comments  Â·  Source: socketio/socket.io

Hey. I am trying to use socket.io with Chrome 17 but it cant connect in 40% of the cases and says "WebSocket is closed before the connection is established.". At times it works perfectly. I am using the latest socket.io version with the latest nodejs, on Windows both, cross domain.

Any ideas?

Cheers

All 33 comments

Any particular antivirus, firewall or proxy service in place?

my users are getting this now as well after leaving 0.8.7

I am getting this exact same error, on local network, no firewall. Just a router between the two computers.

My fix for this was to disable client side caching. I can't explain why, but that fixed it.

was that this?
io.disable('browser client cache');

Oddly yes. But it didn't completely fix it. Its now more like 25% will give me an "WebSocket is closed before the connection is established." In stead of the 40 - 50 % I was getting before.

I thought I would report back to say I finally found my problem. I was hoping to use node's native cluster functionality to load balance the system, but this was killing it. About 25% of the time, socket.io or some part of it would not make it back to the correct instance of node, and then either the browser, or socket.io on node would kill that socket. Thus killing my application.

I hope this helps

I used cluster too. Maybe that's the problem. The thing is I switched to an other websocket module, that works fine.
I have default windows firewall, and avast, but I don't think that's the problem.

I'm using cluster and socket.io version 0.9.9, got exactly same issue 'WebSocket is closed before the connection is established'.

Any plan to fix it on future release? Thanks.

I wrote my initial codes without cluster. And it was working ok.
Then after, I inserted this whole bunch of codes into the boilerplate cluster codes.
This coughs out the above error message.

if(cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) { cluster.fork(); }
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
}
else {

io.sockets.on("connection", function(socket) {
console.log("WORKER INSIDE SOCKET: " + cluster.worker.id);
})

}

i got this error in browser (firefox latest, chrome) ,when i disable heartbeats
io.disable('heartbeats');

Same here @uning .
I disabled heartbeats and got the error. Now I'm looking for a new way to disable heartbeats.

io.disable('heartbeats'); gives me the same error.

Had to switch it back to long-polling :( Any news? I'll have at look next weekend.

Same issue with 0.9.6 in firefox and chrome. But just for some user, any idea?

Encounted same issue...However it was OK before...

@dickeylth which version?

@rase- v0.9.16

I'm using cluster and socket.io version 1.3.5, got exactly same issue 'WebSocket is closed before the connection is established'.

Any plan to fix it on future release???

why this thread closed? problem still rampant.

lol yeh im like 12 hrs on this

A am using latest version and I still have this problem. Although it was working locally, but not on Amazon server (not using ELB!).

@marcinkolodziejczyk what port are you using? Port 80 seems not to work for a lot of people. So 443 with an ssl certificate helps

Yep, it is 80. Curently we do not have a valid certificate to switch to 443.

Application (sails js) runs on port 9000. App is behind Apache which has mod_proxy etc enabled and configured. 80 is being forwarded to 9000 ofcourse.

I will have to try 443. Is this the only option?

You can try 443 without ssl or use a self signed certificate (for testing only) just to confirm that it works. What I learned from testing is that most other ports will work just not port 80

I just tried using 443. Still the same behaviour.

At first, I see "Now connected to Sails...." from sails.io.js:153

Then after few seconds:
"Websocket connection to 'wss://hosthere/socket.io?.....' failed: Websocket is closed before the connection is established. sails.io.js:14

I don't even know where to search for solution. And how. ;/

So, here is some more info regarding this issue. Running in debug mode.

This is what I see in browser.

WebSocket connection to 'wss://APPLICATION_HOST_ADDR/socket.io/?__sails_io_sdk_version=0.13.5&__sai…tform=browser&__sails_io_sdk_language=javascript&EIO=3&transport=websocket' failed: WebSocket is closed before the connection is established.
sails.io.js:14 engine.io-client:socket socket error {"type":"TransportError","description":{"isTrusted":true}} +1ms
sails.io.js:14 socket.io-client:manager connect_error +2ms
sails.io.js:14 socket.io-client:manager cleanup +0ms
sails.io.js:14 socket.io-client:manager reconnect attempt error +1ms
sails.io.js:14 socket.io-client:manager will wait 5000ms before reconnect attempt +0ms
sails.io.js:14 engine.io-client:socket socket close with reason: "transport error" +3ms
sails.io.js:14 engine.io-client:socket socket closing - telling transport to close +0ms
sails.io.js:14 socket.io-client:manager attempting reconnect +5s
sails.io.js:153  
        Socket is trying to reconnect to Sails...
_-|>_-  (attempt #128)


sails.io.js:14 socket.io-client:manager readyState closed +4ms
sails.io.js:14 socket.io-client:manager opening APPLICATION_HOST_ADDR +1ms
sails.io.js:14 engine.io-client:socket creating transport "websocket" +5s
sails.io.js:14 engine.io-client:socket setting transport websocket +1ms
sails.io.js:14 socket.io-client:manager connect attempt will timeout after 20000 +1ms

.... and it repeats

This is what I see in node app with debug on.

engine intercepting request for path "/socket.io/" +10s
  engine handling "GET" http request "/socket.io/?__sails_io_sdk_version=0.13.5&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript&EIO=3&transport=websocket" +0ms
  engine handshaking client "OqC-zDGRqL3taalGAAHu" +0ms
  engine intercepting request for path "/socket.io/" +10s
  engine handling "GET" http request "/socket.io/?__sails_io_sdk_version=0.13.5&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript&EIO=3&transport=websocket" +0ms
  engine handshaking client "j1JksXLKVlXEV3PXAAHv" +1ms
  engine intercepting request for path "/socket.io/" +10s
  engine handling "GET" http request "/socket.io/?__sails_io_sdk_version=0.13.5&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript&EIO=3&transport=websocket" +0ms
  engine handshaking client "qzzox6cDf_1-04T7AAHw" +0ms
  engine intercepting request for path "/socket.io/" +8s
  engine handling "GET" http request "/socket.io/?__sails_io_sdk_version=0.13.5&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript&EIO=3&transport=websocket" +0ms
  engine handshaking client "cTvzzzg74BQ2aqIYAAHx" +0ms

and so on...

Please notice, that client id everytime is different. Is it normal behaviour?

On local machine client is always the same (plus output has many different messages like ping/pong and many more).

Below is what is working locally:

Local on browser:

socket.io-client:url parse http://localhost:9000 +0ms
sails.io.js:14 socket.io-client new io instance for http://localhost:9000 +10ms
sails.io.js:14 socket.io-client:manager readyState closed +2ms
sails.io.js:14 socket.io-client:manager opening http://localhost:9000 +1ms
sails.io.js:14 engine.io-client:socket creating transport "polling" +0ms
sails.io.js:14 engine.io-client:polling polling +1ms
sails.io.js:14 engine.io-client:polling-xhr xhr poll +1ms
sails.io.js:14 engine.io-client:polling-xhr xhr open GET: http://localhost:9000/socket.io/?__sails_io_sdk_version=0.13.5&__sails_io_s…owser&__sails_io_sdk_language=javascript&EIO=3&transport=polling&t=LBg-U8w +2ms
sails.io.js:14 engine.io-client:polling-xhr xhr data null +1ms
sails.io.js:14 engine.io-client:socket setting transport polling +1ms
sails.io.js:14 socket.io-client:manager connect attempt will timeout after 20000 +11ms
sails.io.js:14 socket.io-client:manager readyState opening +2ms
sails.io.js:14 engine.io-client:polling polling got data ArrayBuffer +20ms
sails.io.js:14 engine.io-client:socket socket receive: type "open", data "{"sid":"il9H5LheyH0dqXypAAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}" +37ms
sails.io.js:14 engine.io-client:socket socket open +1ms
sails.io.js:14 socket.io-client:manager open +54ms
sails.io.js:14 socket.io-client:manager cleanup +1ms
sails.io.js:14 socket.io-client:socket transport is open - connecting +1ms
sails.io.js:14 engine.io-client:socket starting upgrade probes +4ms
sails.io.js:14 engine.io-client:socket probing transport "websocket" +1ms
sails.io.js:14 engine.io-client:socket creating transport "websocket" +0ms
sails.io.js:14 engine.io-client:polling polling +4ms
sails.io.js:14 engine.io-client:polling-xhr xhr poll +1ms
sails.io.js:14 engine.io-client:polling-xhr xhr open GET: http://localhost:9000/socket.io/?__sails_io_sdk_version=0.13.5&__sails_io_s…uage=javascript&EIO=3&transport=polling&t=LBg-UA5&sid=il9H5LheyH0dqXypAAAB +5ms
sails.io.js:14 engine.io-client:polling-xhr xhr data null +0ms

local in node

engine intercepting request for path "/socket.io/" +405ms
  engine handling "GET" http request "/socket.io/?__sails_io_sdk_version=0.13.5&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript&EIO=3&transport=polling&t=LBh4s8m" +1ms
  engine handshaking client "fWxt-R1v-6pm7z9LAAAB" +1ms
  engine:socket sending packet "open" ({"sid":"fWxt-R1v-6pm7z9LAAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}) +1ms
  engine:polling setting request +1ms
  engine:socket flushing buffer to transport +0ms
  engine:polling writing "  �0{"sid":"fWxt-R1v-6pm7z9LAAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}" +5ms
  engine:socket executing batch send callback +131ms
  socket.io:server incoming connection with id fWxt-R1v-6pm7z9LAAAB +0ms
  socket.io:client connecting to namespace / +1ms
  socket.io:namespace adding socket to nsp / +18ms
  socket.io:socket socket connected - writing packet +4ms
  socket.io:socket joining room /#fWxt-R1v-6pm7z9LAAAB +5ms
  socket.io:client writing packet {"type":0,"nsp":"/"} +1ms
  socket.io-parser encoding packet {"type":0,"nsp":"/"} +0ms
  socket.io-parser encoded {"type":0,"nsp":"/"} as 0 +1ms
  engine:socket sending packet "message" (0) +0ms
  socket.io:socket joined room /#fWxt-R1v-6pm7z9LAAAB +0ms
  engine upgrading existing transport +139ms
  engine:socket might upgrade socket transport from "polling" to "websocket" +0ms

So, why it is ok on local machine but not on Amazon?
Can anyone help? :)

why was this closed? I am having this issue but on the google cloud platform.

@lcborn4 This is not an issue with Socket IO. I was having the same issue on GKE and it turned out to be a npm module update, that broke my code and caused either a socket hang up or "WebSocket is closed before the connection is established.".

Check your code to make sure you're not seeing a bug in your server code.

Switch to long polling until you figure it out. It's probably the GCP. Websockets is tricky when using third party platforms

@cyberdude for me, it was mongoose closing connections when it shouldn't and not emitting events using the latest version. SocketIO works perfectly over WSS when NodeJS is serving the cert, even with CloudFlare and TCP load balancer (GCP) in front of it!

Guys don't ever use cluster for websocket applications, I spent whole 12 hours trying to configure apache proxy websocket, but in the end it is because of the cluster thing, mine is working now after I changed cluster number to 1 by 'pm2 scale server -i 1', I didn't investigated much about pm2 code, but I have the feeling it is really tough to cluster a network application into multiple pieces while letting them to use one common port.
If you really want to cluster, you can use multiple forks with different port, works as same , then using apache for proxying to one of them(didn't tried with apache yet, but should be possible).

The problem with cluster is related to pm2 only, If you will try to use cluster without pm2 everything will work good.

Was this page helpful?
0 / 5 - 0 ratings