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);
});
2
  • possible duplicate? Might help you Commented Apr 16, 2016 at 23:21
  • @NiCkNewman I can proxy fine but I need to balance on the basis of data received without actually breaking the socket connection. Commented Apr 17, 2016 at 8:38

1 Answer 1

3
+25

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.