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.

2
  • 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 That works for me. If using it with only one domain, the switch-statements can be removed. Commented Mar 21, 2013 at 14:22
  • I got the code from here: 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. Commented Mar 21, 2013 at 17:07

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thx David... and i was wondering why my apps not working on a fresh node.js installation -.-
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.