Question

These are the versions of node and required modules I am using:

  • Node.js: 0.10.16
  • Websocket Library: einaros/ws ws@0.4.28
  • Proxy server: nodejitsu/node-http-proxy http-proxy@0.10.3

When I run the following program my console output looks like this, and doesn't move beyond this point:

$ node app.js
proxy: got upgrade, proxying web request
wss: got connection

Here's the code:

// app.js
// A simple proxying example
//
// Setup websocket server on port 19000
// Setup proxy on port 9000 to proxy to 19000
// Make a websocket request to 9000
//

var WebSocket = require('ws'),
    WebSocketServer = WebSocket.Server,
    proxy = require('http-proxy');

// goes in a loop sending messages to the server as soon as
// the servers are setup
var triggerClient = function() {
    var ws = new WebSocket('ws://localhost:9090/');
    ws.on('open', function() {
        console.log('ws: connection open');
        setInterval(function() {
            ws.send("Hello");
        }, 1000);
    });

    ws.on('message', function(data) {
        console.log('ws: got ' + data);
    });
}

// setup websocket server and a proxy
//
var go = function() {
    // setup a websocket server on port 19000
    //
    var wss = new WebSocketServer({ port: 19000 });
    wss.on('connection', function(ws) {
        console.log('wss: got connection');
        ws.on('message', function(data) {
            console.log('wss: got ' + data);
            ws.send('wss response: ' + data);
        });
    });


    // setup a proxy server
    var server = proxy.createServer(function (req, res, proxy) {
        proxy.proxyRequest(req, res, {
            host: 'localhost',
            port: 19000
        });
    });

    server.on('upgrade', function (req, socket, head) {
        console.log('proxy: got upgrade, proxying web request');
        server.proxy.proxyWebSocketRequest(req, socket, head, {
            host: 'localhost',
            port: 19000
        });
    });

    server.listen(9090, triggerClient);
};

process.nextTick(go);

My problem eventually started when I was trying to use hipache, I then simplified things to node-http-proxy and then finally to this piece of code.

If you change the port the WebSocket client is connecting to from 9090 to 19000 (thereby bypassing the proxy), things seem to work fine.

Any suggestions, pointers, feedback would be greatly appreciated.

Thanks!

Was it helpful?

Solution

The core problem is that the master branch of node-http-proxy is only compatible with node <= 0.8.x (see https://github.com/nodejitsu/node-http-proxy#when-to-use-node-http-proxy): there's a tree that implements support for 0.10.x (see https://github.com/nodejitsu/node-http-proxy/tree/caronte) but it isn't the mainline branch and I haven't found any indication of when it will be merged in and available.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top