2

I'm trying to get node-http-proxy with socket.io to work, but it always falls back to xhr, websockets won't work, although I'm connecting to the upgrade event as described in the node-http-proxy docs.

var httpProxy = require ("http-proxy");

var server = httpProxy.createServer (function (req, res, proxy) {
    var buffer = httpProxy.buffer (req);
    proxy.proxyRequest (req, res, {
        host : "localhost",
        port : 8001,
        buffer : buffer
    });
});
server.listen (80);

server.on('upgrade', function (req, socket, head) {
    server.proxy.proxyWebSocketRequest(req, socket, head, { host : "localhost", port : 8001 });
});

The app obviously runs on localhost:8001 and if I allow all transport methods it will work fine as it uses xhrs. If I force it to use websockets firefox will say

Firefox can't establish a connection to the server at ws://localhost/socket.io/1/websocket/cNp5J80KAWkXqjE6OZOt. @ http://localhost/socket.io/socket.io.js:2371

Just using the default method

httpProxy.createServer (8001, "localhost").listen (80);

results in the same error.

tom
  • 692
  • 6
  • 12
  • You don't have the same code at all as I have... Can you link the docs where you found this code? Meanwhile you can take a look at my code in [this answer](http://stackoverflow.com/questions/15411042/how-to-run-node-js-app-on-port-80-using-http-proxy/15427378#15427378) That works for me. If using it with only one domain, the switch-statements can be removed. – Andreas Hultgren Mar 21 '13 at 14:22
  • I got the code from here: https://github.com/nodejitsu/node-http-proxy#proxying-websockets (the buffer line I had there was a leftover from an experiment, I removed it now :/ ) With your code I still have the same problem. Could it be that the versions are making problems? I'm running node-http-proxy 0.10.0, socket.io 0.9.13 and node 0.10.0. – tom Mar 21 '13 at 17:07

2 Answers2

4

Proxying websockets with node-http-proxy seems to be broken on Node 0.10. Here's a pull request that tries to fix it, but see my comments: the fix doesn't fully work either.

https://github.com/nodejitsu/node-http-proxy/pull/402

David Glasser
  • 1,438
  • 13
  • 21
  • Thx David... and i was wondering why my apps not working on a fresh node.js installation -.- – ayk Apr 20 '13 at 16:12
2

I am currently using the (latest? v1.0.1) node-http-proxy and i found Websockets were indeed working reliably after a tweak (see below) on Node v0.10.20. By default, node-http-proxy seems to support xhr-polling, while dropping websocket connections.

In the 'examples', i added the 'ws' flag true

var http = require('http'),
    httpProxy = require('../../lib/http-proxy');

httpProxy.createServer({
  target:'http://somehost.tld:5000',
  ws : true //added Websockets Flag TRUE
}).listen(80);//arbitrary port
RononDex
  • 4,143
  • 22
  • 39
bassr3hab
  • 21
  • 1