Node Http Proxy Web Socket Balance
Asked Answered
A

1

7

I need to able to balance websocket on application level. Lets say forward websocket request on the basis of message I received, decode it on proxy and then using that data send to another socket server using some logic.

But I am unable to do this. This is the initial code I wrote and trying to do that. This is the server

var http = require('http');
var httpProxy = require('http-proxy');
var WebSocket = require('ws');

var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
    ws.on('message', function incoming(message) {
        console.log('received: %s', message);
    });
    ws.send('something');
});

var proxy = httpProxy.createServer({target: 'ws://localhost:8080', ws:true});


var server = httpProxy.createServer(function(req, res) {
    //SOME LOGIC HERE TO PARSE WS DATA AND SEND TO WS SERVER
    proxy.ws(req, res, { target: 'ws://localhost:8080'});
}).listen(8014);

CLIENT

var http = require('http');
var httpProxy = require('http-proxy');
var WebSocket = require('ws');


var ws = new WebSocket('ws://localhost:8014/');

ws.on('open', function () {  
    ws.send("CLIENT");
});

ws.on('message', function (msg) {  
    console.log(msg);
});
Apanage answered 16/4, 2016 at 7:36 Comment(2)
possible duplicate? Might help youCrapulent
@NiCkNewman I can proxy fine but I need to balance on the basis of data received without actually breaking the socket connection.Apanage
M
3

Here's an example. In this case the client connects directly to outer.js which then forwards connections to upstream servers (inner.js).

outer.js

var http = require('http');
var httpProxy = require('http-proxy');

var proxies = {
  foo: new httpProxy.createProxyServer({
    target: {
      host: "foo.com",
      port: 8080
    }
  }),
  bar: new httpProxy.createProxyServer({
    target: {
      host: "bar.com",
      port: 8080
    }
  })
  // extend this...
};

var findUpstream = function(req){
  // TODO return key for lookup in @proxies
};

var proxyServer = http.createServer(function (req, res){
  var upstream = findUpstream(req);
  proxies[upstream].web(req, res);
});

proxyServer.on('upgrade', function (req, socket, head) {
  var upstream = findUpstream(req);
  proxies[upstream].ws(req, socket, head);
});

proxyServer.listen(8014);

inner.js

var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
    ws.on('message', function incoming(message) {
        console.log('received: %s', message);
    });
    ws.send('something');
});

In this example you'll need to fill in the findUpstream to return the key such as foo or bar based on data in the request. It'd also be worth putting some error handling in for when the proper upstream server isn't found, but this should illustrate the general idea.

Moffatt answered 19/4, 2016 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.