{"id":20626599,"url":"https://github.com/node-m2m/edge-load-balancing","last_synced_at":"2026-03-09T12:02:56.864Z","repository":{"id":158677495,"uuid":"617120763","full_name":"node-m2m/edge-load-balancing","owner":"node-m2m","description":"A quick demo on how to create a load balancer server for edge computing.","archived":false,"fork":false,"pushed_at":"2025-02-28T00:55:09.000Z","size":115,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-28T07:58:06.090Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/node-m2m.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-21T18:34:18.000Z","updated_at":"2025-02-28T00:55:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"65f10ed3-afe9-49b8-b4ad-a4f061689503","html_url":"https://github.com/node-m2m/edge-load-balancing","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-m2m%2Fedge-load-balancing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-m2m%2Fedge-load-balancing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-m2m%2Fedge-load-balancing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-m2m%2Fedge-load-balancing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/node-m2m","download_url":"https://codeload.github.com/node-m2m/edge-load-balancing/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242483345,"owners_count":20135784,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T13:14:08.637Z","updated_at":"2026-03-09T12:02:51.818Z","avatar_url":"https://github.com/node-m2m.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\n## Edge Load Balancer\n![](assets/edge-loadbalancer.png)\n\n\u003cbr\u003e\nIn this example, the client will monitor the data 'test-data' topic from an edge load balancer server. \nThe load balancer will fetch the data from 3 sources (server1, server2, and server3) one at a time using a simple round robin method.   \nIt will then send the fetched data one at a time to the client or to every connected clients. \n\n\nIf one of the load balancer 3 sources of data goes down due to some unexpected events, it will continue to provide the data from the other sources.    \n\n#### Create a project directory for each edge endpoint and install *m2m*.\n```js\n$ npm install m2m\n```\n### Server 1\n#### Save the code below as *app.js* in your server1 project directory.\n\n```js\nconst m2m = require('m2m')\n\nlet edge = new m2m.Edge({name:'server1'})\n\n// simulated data source\nfunction dataSource(){\n  return 20 + Math.floor(Math.random() * 10)\n}\n\nm2m.connect(() =\u003e {\n\n  let port = 8144\n\n  edge.createServer(port, '127.0.0.1', (server) =\u003e {\n\n    server.on('error', (error) =\u003e { \n      console.log('error:', error)\n    })\n\n    server.dataSource('test-data', (tcp) =\u003e {\n      tcp.send({server:1, topic:tcp.topic, value:dataSource()})         \n    })\n  })\n})\n```\n### Server 2\n#### Save the code below as *app.js* in your server2 project directory.\n\n```js\nconst m2m = require('m2m')\n\nlet edge = new m2m.Edge({name:'server2'})\n\n// simulated data source\nfunction dataSource(){\n  return 20 + Math.floor(Math.random() * 10)\n}\n\nm2m.connect(() =\u003e {\n\n  let port = 8145\n\n  edge.createServer(port, '127.0.0.1', (server) =\u003e {\n\n    server.on('error', (error) =\u003e { \n      console.log('error:', error)\n    })\n\n    server.dataSource('test-data', (tcp) =\u003e {\n      tcp.send({server:2, topic:tcp.topic, value:dataSource()})             \n    })\n  })\n})\n```\n### Server 3\n\n#### Save the code below as *app.js* in your server3 project directory.\n```js\nconst m2m = require('m2m')\n\nlet edge = new m2m.Edge({name:'server3'})\n\n// simulated data source\nfunction dataSource(){\n  return 20 + Math.floor(Math.random() * 10)\n}\n\nm2m.connect(() =\u003e {\n\n  let port = 8146\n\n  edge.createServer(port, '127.0.0.1', (server) =\u003e {\n\n    server.on('error', (error) =\u003e { \n      console.log('error:', error)\n    })\n\n    server.dataSource('test-data', (tcp) =\u003e {\n      tcp.send({server:3, topic:tcp.topic, value:dataSource()})       \n    })\n  })\n})\n```\n### Load Balancer\n\n#### Save the code below as *app.js* in your load balancer project directory.\n```js\nconst m2m = require('m2m')\n\nlet edge = new m2m.Edge({name:'Load Balancer'})\n\nm2m.connect(() =\u003e {\n\n  let ec1 = new edge.client(8144, '127.0.0.1')\n  let ec2 = new edge.client(8145, '127.0.0.1')\n  let ec3 = new edge.client(8146, '127.0.0.1')\n\n  let port = 8143, data = null, round = 1  \n\n  edge.createServer(port, '127.0.0.1', (server) =\u003e {\n\n    server.on('connection', (count) =\u003e { \n      console.log('connected client', count)\n    })\n\n    server.publish('test-data', async (tcp) =\u003e {\n      if(round === 1){\n        round = 2\n        data = await ec1.read(tcp.topic)\n      }\n      else if(round === 2){\n        round = 3\n        data = await ec2.read(tcp.topic)\n      }\n      else if(round === 3){\n        round = 1\n        data = await ec3.read(tcp.topic)\n      }\n      tcp.send(data)\n    })\n  })\n})\n```\n### Client\n\n#### Save the code below as *app.js* in your client project directory.\n```js\nconst m2m = require('m2m')\n\nlet edge = new m2m.Edge({name:'client'})\n\nasync function main (){\n\n  await m2m.connect();\n\n  let ec = new edge.client(8143, '127.0.0.1')\n\n  ec.on('ready', (result) =\u003e { \n    console.log('ready:', result)\n  })\n\n  ec.on('error', (error) =\u003e { \n    console.log('error:', error)\n  })\n\n  ec.subscribe('test-data', (data) =\u003e {\n    console.log(data)\n  })\n}\n```\n\n\u003cbr\u003e\n\n#### Start the application on each endpoint.\n```js\n$ node app.js\n```\n\n\u003cbr\u003e\n\nYou should get a client output similar to the result as shown below.\n```js\nready: true\n{ server: 1, topic: 'test-data', value: 27 }\n{ server: 2, topic: 'test-data', value: 25 }\n{ server: 3, topic: 'test-data', value: 22 }\n...\n\n\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-m2m%2Fedge-load-balancing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnode-m2m%2Fedge-load-balancing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-m2m%2Fedge-load-balancing/lists"}