44

I am using HAProxy to send requests, on a subdomain, to a node.js app.

I am unable to get WebSockets to work. So far I have only been able to get the client to establish a WebSocket connection but then there is a disconnection which follows very soon after.

I am on ubuntu. I have been using various versions of socket.io and node-websocket-server. The client is either the latest versions of Safari or Chrome. HAProxy version is 1.4.8

Here is my HAProxy.cfg

global maxconn 4096 pidfile /var/run/haproxy.pid daemon defaults mode http maxconn 2000 option http-server-close option http-pretend-keepalive contimeout 5000 clitimeout 50000 srvtimeout 50000 frontend HTTP_PROXY bind *:80 timeout client 86400000 #default server default_backend NGINX_SERVERS #node server acl host_node_sockettest hdr_beg(host) -i mysubdomain.mydomain use_backend NODE_SOCKETTEST_SERVERS if host_node_sockettest backend NGINX_SERVERS server THIS_NGINX_SERVER 127.0.0.1:8081 backend NODE_SOCKETTEST_SERVERS timeout queue 5000 timeout server 86400000 server THIS_NODE_SERVER localhost:8180 maxconn 200 check 

I've trawled the web and mailing list but can not get any of the suggested solutions to work.

(p.s. this could be for serverfault, but there are other HAProxy question on S.O, so I have chosen to post here)

4 Answers 4

60

Upgrade to latest version of socket.io (0.6.8 -> npm install [email protected], which is patched to work with HAProxy) and download the latest version of HAProxy.

Here is an example config file:

global maxconn 4096 # Total Max Connections. This is dependent on ulimit nbproc 2 defaults mode http frontend all 0.0.0.0:80 timeout client 5000 default_backend www_backend acl is_websocket hdr(Upgrade) -i WebSocket acl is_websocket hdr_beg(Host) -i ws use_backend socket_backend if is_websocket backend www_backend balance roundrobin option forwardfor # This sets X-Forwarded-For timeout server 5000 timeout connect 4000 server server1 localhost:8081 weight 1 maxconn 1024 check server server2 localhost:8082 weight 1 maxconn 1024 check server server3 localhost:8083 weight 1 maxconn 1024 check backend socket_backend balance roundrobin option forwardfor # This sets X-Forwarded-For timeout queue 5000 timeout server 5000 timeout connect 5000 server server1 localhost:8081 weight 1 maxconn 1024 check server server2 localhost:8082 weight 1 maxconn 1024 check server server3 localhost:8083 weight 1 maxconn 1024 check 
Sign up to request clarification or add additional context in comments.

6 Comments

Which version of HAProxy are you using?
@Diego you can use any version > 1.4
Does this allow sticky sessions with authentication? I've added a separate question for the issue: stackoverflow.com/q/8149038/152541
Guys, please do not use a one day connect timeout, it will cause connections to accumulate if one of your server becomes temporarily unreachable. A few seconds is more than enough !
This solution will break if a browser client falls back onto the xhr-polling or any other long polling style transport. For that to work you need the http backend to either use either a cookie or a source ip hash (balance source) method to make sure all requests hit the same backend server, even with the redisstore.
|
6

It's likely that your client is using WebSockets version 76. In which case you can't use "mode http" because the WebSockets handshake violates HTTP. There seems to be ambivalence in the committee about whether the WebSockets handshake should be compatible with HTTP or not. Anyways, the problem with the v76 handshake is that raw data is sent with the handshake (the checksum chunk).

The relevant HAProxy discussion: http://www.mail-archive.com/[email protected]/msg03046.html

From the discussion it sounds like there might be a way to default to TCP mode and fall back to HTTP for non-WebSockets connections.

3 Comments

Great info. Sounds like there is a desire to have websockets off port 80 which I am trying to accomplish here. Will look into the TCP layer 4 mode.
@Ross, please post back what you find. Or if you don't come to another resolution can you select this answer so that anybody else searching for the same issue knows that there is a resolution when scanning the list of questions.
It looks like some libraries are trying to work around this. Relevant NodeJS commit: github.com/Worlize/Socket.IO-node/commit/…
5

We are using a Netty implementation https://github.com/ibdknox/socket.io-netty and here is the HAProxy file that worked for us. The trick to get it not to fall back to XHR-Polling but use Websockets is putting HAProxy into TCP mode. HAProxy config:

global daemon maxconn 32000 defaults mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms listen http-in bind *:80 server server1 1.1.1.1:8000 check server server2 1.1.1.1:8000 check listen socketio-in mode tcp bind *:8080 balance source timeout queue 5000 timeout server 86400000 timeout connect 86400000 server server1 1.1.1.1:8080 check server server2 1.1.1.1:8080 check 

Where 1.1.1.1 is your IPs

Comments

-1

Try using Socket.io instead of node-websockets-server, it's an abstraction layer with fallbacks to many different methods of instant communication between browser and server.

Whilst it's true WebSockets violate HTTP 1.0, they do not violate HTTP 1.1, so you should be able to proxy them with any server capable of proxying HTTP 1.1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.