{"id":18396662,"url":"https://github.com/rob--/cluster-messages","last_synced_at":"2025-04-07T04:32:44.900Z","repository":{"id":57200976,"uuid":"91750090","full_name":"Rob--/cluster-messages","owner":"Rob--","description":"Easily send messages between workers and the master with callbacks in Node.js","archived":false,"fork":false,"pushed_at":"2024-04-06T03:55:14.000Z","size":23,"stargazers_count":16,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-03T14:38:12.711Z","etag":null,"topics":["clusters","event-listener","eventemitter","javascript","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/Rob--.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2017-05-19T00:45:04.000Z","updated_at":"2025-03-30T01:09:31.000Z","dependencies_parsed_at":"2024-04-06T03:25:09.267Z","dependency_job_id":"b901ab15-8011-40ae-9360-672e14b6e8d9","html_url":"https://github.com/Rob--/cluster-messages","commit_stats":{"total_commits":8,"total_committers":2,"mean_commits":4.0,"dds":0.25,"last_synced_commit":"ad9465fce6ab19799473b46ae9f5197716f79a69"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rob--%2Fcluster-messages","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rob--%2Fcluster-messages/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rob--%2Fcluster-messages/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rob--%2Fcluster-messages/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Rob--","download_url":"https://codeload.github.com/Rob--/cluster-messages/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247595074,"owners_count":20963939,"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":["clusters","event-listener","eventemitter","javascript","nodejs"],"created_at":"2024-11-06T02:14:30.211Z","updated_at":"2025-04-07T04:32:44.557Z","avatar_url":"https://github.com/Rob--.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cluster-messages\nA helpful Node module to make it easy to send messages between the\nmaster and workers with callbacks.\n\nEvents can be emitted by any process and received by any process.\n\nTODO:\n1. Investigate more efficient serialisation/transmission\n2. Implement logger class\n3. Implement emitOnce (where callback is garbage collected after all workers respond)\n4. Update README\n5. Add tests\n6. Implement await/async\n7. Add linter\n8. Convert to TypeScript\n9. Transpile with Babel\n10. Deep merge default options\n11. Support modular architecture to move messages off IPC wire\n  * Support HTTP, web sockets, shared memory?\n12. Introduce load testing benchmarks\n\n# Usage\n\nRequire the package:\n```javascript\nlet ClusterMessages = require('cluster-messages');\nlet messages = new ClusterMessages();\n```\n\nSetting up event listeners:\n```javascript\nmessages.on('multiplication', (data, sendResponse) =\u003e {\n  sendResponse(data.x * data.y);\n});\n```\n\nEmitting events:\n```javascript\nlet data = {\n  x: Math.round(Math.random() * 100),\n  y: Math.round(Math.random() * 100),\n};\n\nmessages.send('multiplication', data, response =\u003e {\n  console.log(`${data.x} * ${data.y} = ${response}`)\n})\n```\n\n---\n\n## .send(eventName, data, callback)\n\n- **eventName** (string) - the name of the event being emitted\n- **data** (object) - the object passed to the event listener\n- **callback** (function) - callback invoked by the event listener, single parameter containing the response\n\n```javascript\nmessages.send('print', { name: 'John' }, (response) =\u003e {\n  console.log(response); // 'Hi John'\n});\n```\n\nThis function will emit an event, the callback is a single parameter that is passed, this means inside the event listener only one parameter can be passed to `sendResponse` so it needs to be an object containing all the data.\n\nWhen sent from the master:\n- the event will get sent to all workers\n- each time the worker calls `sendResponse` (the callback inside the event listener), the `callback` will be invoked\n- e.g. if there are 3 workers, and all of them call `sendResponse`, the callback will be invoked 3 times\n\nWhen sent from the worker:\n- the event is sent to the master only\n\n## .on(eventName, callback)\n\n- **eventName** (string) - the name of the event to listen for\n- **data** (object) - single object sent by the event emitter (`.send`)\n- **callback** (function) - takes two parameters, `data` and `sendResponse` (function). `data` is the data sent by the event emitter, `sendResponse` is a function that you invoke and pass the data you want to send back to the callback on the event emitter\n\n```javascript\nmessages.on('print', (data, sendResponse) =\u003e {\n  sendResponse('Hi ' + data.name);\n});\n```\n\nThis is the event listener, the callback takes two parameters (`data` and `sendResponse`). `sendResponse` only takes one parameter and is sent back to wherever the event originated.\n\n---\n\nWhen initiating ClusterMessages you can pass it some options:\n```javascript\nconst ClusterMessages = require('cluster-messages');\n\nconst instanceName = 'healthMonitor';\nconst options = {\n  metadataKey: '__metadata__',\n  log: {\n    level: 'warn',\n    type: 'hash'\n  }\n};\n\nconst messages = new ClusterMessages(instanceName, options);\n```\n\n#### metadataKey\nMessages are sent around as usual and can be received through `cluster.on('message')` or `process.on('message')` as expected, so the module adds a property containing metadata that is passed around with each event. As this metadata property is essentially exposed in every single `message` event emission within `cluster` or `process`, the `metadataKey` option allows you to define the name of the meta data property to ensure it does not conflict with your application.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frob--%2Fcluster-messages","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frob--%2Fcluster-messages","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frob--%2Fcluster-messages/lists"}