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
$ 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
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
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
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' }
...