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);
});
A reverse HTTP proxy over WebSocket is a type of proxy server which uses the WebSocket protocol as a "tunnel" to pass TCP communication from server to client. In Go project, gorilla/websocket is widely used to implement WebSocket. WebSocket is designed to work over HTTP.
Node. js can maintain many hundreds of WebSockets connections simultaneously. WebSockets on the server can become complicated as the connection upgrade from HTTP to WebSockets requires handling. This is why developers commonly use a library to manage this for them.
The theoretical limit is 65k connections per IP address but the actual limit is often more like 20k, so we use multiple addresses to connect 20k to each (50 * 20k = 1 mil). I then run the web server as root by typing sudo -i followed by ulimit -n 1024000 and then node examples/WebSocket. js (in the uWebSockets.
node-http-proxy. node-http-proxy is an HTTP programmable proxying library that supports websockets. It is suitable for implementing components such as reverse proxies and load balancers.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With