Git Product home page Git Product logo

gateway-load-balancer's Introduction

Gateway Load-Balancer

Overview

In this quick demo, everytime a client tries to access the gateway it will receive resources for each remote server in a round-robin method.

Ideally each application process should be running on separate nodes or computers (5 computers).

However for this demo you can just run each process in one computer.

You need to register 4 devices using the device id of 1200, 100, 200, 300.

You can follow the step-by-step procedure and create the necessary directories as you go along with this tutorial.

Or you can just git clone the project repository and install m2m to each directory components.

To git clone this project, use the command below.

$ git clone https://github.com/EdAlegrid/gateway-load-balancer.git

Procedure

1. Create a gateway directory and install m2m

$ npm install m2m

Save the code below as device.js to run your gateway.

'use strict';

const m2m = require('m2m');

let device = new m2m.Device(1200);

let server = 1;

function loadServer(id, data){
  device.getData({id:id, channel:'test-data'}, (d) => {
    if(d){
      setImmediate(() => {
        data.send(d);
      });
    }
  });
}

device.connect(() => {
  // Set server balancer 1200
  device.setChannelData('sb-1200', (data) => { 
    if(server === 1){
      server = 2;
      loadServer(100, data);
    }
    else if(server === 2){
      server = 3;
      loadServer(200, data);
    }
    else if(server === 3){
      server = 1;
      loadServer(300, data);
    }
  });
});

Start your gateway application.

$ node device.js

2. Create server1 directory and install m2m

Save the code below as device.js to run your 1st server.

'use strict';

const m2m = require('m2m');

let server = new m2m.Device(100);

server.connect(() => {
  server.setChannelData('test-data', (data) => {
    data.send({msg:'hello from server 100'});
  });
});

Start server1 application.

$ node device.js

3. Create server2 and server3 directories and install m2m

Follow the same procedure as with server1.

Save the same code but change the deviceId to 200 and 300 respectively instead of 100.

Start each server the same way with server1.

$ node device.js

4. Create a client directory and install m2m

Save the code below as client.js.

'use strict';

const m2m = require('m2m');

let client = new m2m.Client();

client.connect(() => {
  client.watchChannelData({id:1200, channel:'sb-1200'}, (data) => {
    console.log('watch gateway sb-1200', data);
  });
});

Start your client application.

$ node client.js

You should get an output result similar below.

$ 
watch gateway sb-1200 { msg: 'hello from server 100' }
watch gateway sb-1200 { msg: 'hello from server 200' }
watch gateway sb-1200 { msg: 'hello from server 300' }
watch gateway sb-1200 { msg: 'hello from server 100' }
watch gateway sb-1200 { msg: 'hello from server 200' }
watch gateway sb-1200 { msg: 'hello from server 300' }
...

gateway-load-balancer's People

Contributors

edalegrid avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.