Node http proxy with proxytable and websockets
Asked Answered
C

1

8

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)

Celebrate answered 12/5, 2013 at 1:34 Comment(0)
O
2

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].)

Octavus answered 15/5, 2013 at 20:46 Comment(5)
I have tried this, with and without the upgrade nothing happens, I dont even get errors either & eventually it crashes. I'm using the proxy table example and just modified the hostnames to other ones, no other editsCelebrate
@Akshat: Further research reveals it's actually an incompatibility with node 0.10. See update.Octavus
I see, I also had the issue with 0.8.2 with the recent update. Is there no workaround for this?Celebrate
@Akshat: There was a patch in the most recent release of http-proxy intended to fix the issue (at least temporarily), but it is not a complete fix; that's why socket.io doesn't work and why that version doesn't seem to work in node v0.8. Try installing the previous release of http-proxy, 0.10.1.Octavus
I tried downgrading to node 0.8.23 but the websocket still wont connect (I get a 400 error in chrome), the memory leak errors do however disappear.Celebrate

© 2022 - 2024 — McMap. All rights reserved.