Node http proxy with proxytable and websockets

I'm trying to get websockets to also work with node-http-proxy. The difference is i'm using a proxytable:

var options = {
router: {
    'a.websterten.com': '127.0.0.1:150',
    'b.websterten.com' : '127.0.0.1:151',
}
};

var server = httpProxy.createServer(options);

I tried:

server.on('upgrade', function (req, socket, head) {
    server.proxy.proxyWebSocketRequest(req, socket, head);
});

But it doesn't seem to work. A quick check to see whether websockets work shows I get Unexpected response code: 400 from Chrome (works fine if I go directly)

Also doing a couple of checks server.on('upgrade',.. doesn't fire on a websocket request

How can I get my proxy server to route websockets correctly?

I've also tried this on node 0.8.23 as well as node 0.10.x (the later versions of node have a memory leak issue, but it wont work on 0.8.23 either)

Answers:

Answer

When you use httpProxy.createServer(), it's not necessary to handle the upgrade event because http-proxy handles it automatically. Thus, your server.on('upgrade', ...) never fires because http-proxy is already handling it internally.

The only time you need to do server.on('upgrade') is when you pass middleware functions to httpProxy.createServer or if you've manually created the server with http.createServer().

In other words, websockets should "just work" through the proxy in your configuration.


However, WebSocket support in http-proxy is currently broken on node v0.10.x because of streams2 (the stream APIs in node core were completely rewritten in 0.10). Also, the most recent release of http-proxy (0.10.2) is broken in node v0.8 because of a botched fix for the streams2 issue.

So you have two options:

  1. Wait for http-proxy to rewrite its internals to cope with streams2.
  2. Downgrade to node v0.8.23 and http-proxy 0.10.1. (At least until #1 happens.)

(You can install old versions of npm modules by running npm install [email protected].)

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.