Socket.io session ends with HAproxy

haproxynode.jssocket

I'm stuck with dealing with the configuration of HAproxy to make it work with socket.io.

My last question was this: HAProxy is caching the forwarding? and I got it working, but for some reason, after the authentication is made and the some commands are sent, the connection is dropped and a new connection is made as you can see here:

  info  - handshake authorized 2ZqGgU2L5RNksXQRWuhi
  debug - setting request GET /socket.io/1/websocket/2ZqGgU2L5RNksXQRWuhi
  debug - set heartbeat interval for client 2ZqGgU2L5RNksXQRWuhi
  debug - client authorized for 
  debug - websocket writing 1::
  debug - websocket received data packet 5:3+::{"name":"ferret","args":["tobi"]}
  debug - sending data ack packet
  debug - websocket writing 6:::3+["woot"]
  info  - transport end (socket end)
  debug - set close timeout for client 2ZqGgU2L5RNksXQRWuhi
  debug - cleared close timeout for client 2ZqGgU2L5RNksXQRWuhi
  debug - cleared heartbeat interval for client 2ZqGgU2L5RNksXQRWuhi
  debug - discarding transport
  debug - client authorized
  info  - handshake authorized WkHV-B80ejP6MHQTWuhj
  debug - setting request GET /socket.io/1/websocket/WkHV-B80ejP6MHQTWuhj
  debug - set heartbeat interval for client WkHV-B80ejP6MHQTWuhj
  debug - client authorized for 
  debug - websocket writing 1::
  debug - websocket received data packet 5:4+::{"name":"ferret","args":["tobi"]}
  debug - sending data ack packet
  debug - websocket writing 6:::4+["woot"]
  info  - transport end (socket end)

I tried several configurations, something like this: https://stackoverflow.com/questions/4360221/haproxy-websocket-disconnection/

and also declaring 2 backends, and using ACL to forward to a backend that has the
option http-pretend-keepalive
when the request is a websocket request and to a backend that has http-server-close when the request is only for socket.io static files or is any other type of request that is not websocket.

I would clarify that http-server-close is only on the nginx backend and in the static files backend, http-pretend-keepalive is on frontend all and in the websocket backend.

Can anyone could point me in the right direction? I tried several combinations and none worked so far.

Best Answer

got an answer from the HAproxy mailling list, where an user called "Baptiste" give me this link:

http://blog.exceliance.fr/2012/11/07/websockets-load-balancing-with-haproxy/

that configuration works perfectly with socket.io (tested with all the protocols except flashsockets). What make it works is the set of options in the default directive, the checks for websockets in the backends and the headers checks are not really needed and you can set your own ACL to whatever you need to decide if you use the backend that is executing the socket.io app or not.

The stable version of HAproxy doesn't work with this config, you need a 1.5-devel10 or newer to have it working (i used the 1.5-devel14 version).