Search code examples
node.jssocket.ionode-http-proxy

node-http-proxy socket.io websocket connection


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.


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