Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top