nodejitsu/node-http-proxy — Issue #202: how to scale multi websocket server?
Author: Jxck
Created: 2012-02-24 16:28:41+00:00
State: open

Hi forks.

I want to use node-http-proxy for revers proxy in front of socket.io server.

scale to 3 socket.io server and redis.
like this

```
---- socket.io:3000 ----
| |
client -- http-proxy:4000 +---- socket.io:3001 ----+---- redis
| |
---- socket.io:3002 ----

```

redis is for sharing socket.io broadcast with pub/sub.
socket.io server is stands on express server.

and I wrote proxy setting like this.

```javascript
var http = require('http')
, httpProxy = require('http-proxy')
;

var addresses = [
{ host: 'localhost', port: 3000 }
, { host: 'localhost', port: 3001 }
, { host: 'localhost', port: 3002 }
];

function target() {
var rand = Math.floor(Math.random() * (addresses.length));
return addresses[rand];
}

var server = http.createServer(function (req, res) {
var proxy = new httpProxy.HttpProxy({ target: target() }); // A
proxy.proxyRequest(req, res);
});

server.on('upgrade', function(req, socket, head) {
var proxy = new httpProxy.HttpProxy({ target: target() }); // B
proxy.proxyWebSocketRequest(req, socket, head)
});

server.listen(4000);
```

but this has a problem.
websocket request needs http request first.
so first request is proxyed by proxy object at A.
and upgrade requrest comming to server,
but proxy object at B is different target from A,
because I choose target server in random.

I want to use same target proxy object at B
but I can't get

"which target this connection looking for?"

how to write in this case?

sorry for my poor english :p