1 year ago

#366117

test-img

mundeepsinghcode

Websocket load balancing using nginx within a node js client and server command line application

I am creating a app where I can run three different nodejs servers and have a client connect to one of the three. However, in case there are is more load on one server than the other, I want to use nginx server to proxy all requests to a different node server on a different port.

Below is my client.js

#!/usr/bin/env node
var WebSocketClient = require('websocket').client;

var client = new WebSocketClient();

client.on('connectFailed', function(error) {
    console.log('Connect Error: ' + error.toString());
});

client.on('connect', function(connection) {
    console.log('WebSocket Client Connected');
    connection.on('error', function(error) {
        console.log("Connection Error: " + error.toString());
    });
    connection.on('close', function() {
        console.log('echo-protocol Connection Closed');
    });
    connection.on('message', function(message) {
        if (message.type === 'utf8') {
            console.log("Received: '" + message.utf8Data + "'");
        }
    });

});

client.connect('ws://localhost:8080/', 'echo-protocol');

server.js

#!/usr/bin/env node
var WebSocketServer = require('websocket').server;
var http = require('http');

var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(404);
    response.end();
});
server.listen(8080, function() {
    console.log((new Date()) + `Server is listening`);
});

wsServer = new WebSocketServer({
    httpServer: server,
    autoAcceptConnections: false
});

function originIsAllowed(origin) {
  return true;
}

wsServer.on('request', function(request) {
    if (!originIsAllowed(request.origin)) {
      request.reject();
      console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
      return;
    }
    
    var connection = request.accept('echo-protocol', request.origin);
    console.log((new Date()) + ' Connection accepted.');
    connection.on('message', function(message) {
        if (message.type === 'utf8') {
            console.log('Received Message: ' + message.utf8Data);
            connection.sendUTF(message.utf8Data);
        }
        else if (message.type === 'binary') {
            console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
            connection.sendBytes(message.binaryData);
        }
    });
    connection.on('close', function(reasonCode, description) {
        console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
    });
});

I know nginx is primiarly used for webpages but I was wondering if it could be used for something like this when creating an application where you are connecting to a server and there is too much load, you are proxied to a new port. If so, would something like this work?

upstream nodes {
    ip_hash;
    server ws://localhost:8080/;
    server ws://localhost:8081;
    server ws://localhost:8082;
}

server {
    listen 3000;

    location / {
        proxy_pass http://nodes;
    }
}

Any help would be appreciated or sense of direction.

Thank you

node.js

sockets

nginx

socket.io

load-balancing

0 Answers

Your Answer

Accepted video resources