2

I am developing a nodeJS websocket proxy server. The use case is when a websocket request comes, I will check its credentials, add new headers and then based on its group (from user id) to redirect the websocket connection to its target webscoket server. I found most of the packages (such as node-http-proxy) supports single target. Is there a package supporting multiple targets ?

Conrad
  • 53
  • 2

2 Answers2

0

From the README of node-http-proxy section, "Setup a stand-alone proxy server with custom server logic":

var server = http.createServer(function(req, res) {
  // You can define here your custom logic to handle the request
  // and then proxy the request.
  proxy.web(req, res, { target: 'http://127.0.0.1:5060' });
});

Therefore, you should be able to add your decision code in the callback function and then make a proxy.web() call with your determined target.

  • 1
    It works. I ended with using: server.on('upgrade', function (req, socket, head) { proxy.ws(req, socket, head, {target: 'ws://echo.websocket.org', changeOrigin: true, ws: true}, function(e) { console.log(e) }); }); }); – Conrad Sep 20 '17 at 14:54
0

Maybe it will help. What I did in that case

proxyServer.on('upgrade', function (req, socket, head) {
    const target = getTargetRoute(req)
    if (_.isNil(target)) {
      logger.error(`No target`)
      return
    }
    proxy.ws(req, socket, head, { target })
  })