Nur Rony
Nur Rony

Reputation: 8083

Why socket.io thowing this error?

I am facing a problem which was not before there and everything was working as it should be even yesterday. but today suddenly i see socket.io is showing this error on console

failed:Connection closed before receiving a handshake response

Does anybody know what is the reason?

On Serverside

socket.on('initUser',function(udata){
   //doing some work
   socket.emit('message',{userId:user.id});
});

On ClientSide

socket.on('connect',function () {
   socket.emit('initUser',{data:udata});
});
socket.on('message', function (data) {
  //doing ui task on returned data
});

Thanks in Advance

Upvotes: 0

Views: 3258

Answers (1)

Paris
Paris

Reputation: 6771

It seems that this is caused by a bug in the latest versions of Apache (>= 2.4), when using ProxyPass to (reverse) proxy WebSocket connections to an upstream server. I read more about this incident at a mailing list, but I found no further explanation about why exactly Apache rejects —seemed to be— random connections.

I was dealing with the same issue, at random times, when trying to connect to a Tornado WebSocket app, through Apache, and the Apache error and access logs didn't contain any info about that.

My approach on solving that

Since I couldn't find a way to make the back-end fail safe, I decided to solve the issue in the front-end, with JavaScript. So I added an attribute connected in my WebSocket object, which was by default false and was being set as true when the connection opened. Then I added an event handler on the close event, which retried the connection if it was never established.

Code example

var sock; 

function connect () {
  sock = new WebSocket('ws://myhost');

  sock.connected = false;

  sock.addEventListener('open', function () {
    this.connected = true;
  });

  sock.addEventListener('close', function () {
    if (!this.connected) {
      connect();
    }
  });

}

connect();

I hope this helped you.

Upvotes: 3

Related Questions