# worker_threads_clusters

> Split CPU-intensive tasks on multiple servers through node worker_threads.

Latest version **0.0.6** (published 2022-10-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install worker_threads_clusters
pnpm add worker_threads_clusters
yarn add worker_threads_clusters
bun add worker_threads_clusters
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2022-10-25 |
| First published | 2022-10-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 123.8 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Hackermon |
| Maintainers | hackermondev |
| Keywords | worker_threads, cpu, typescript |

## Links

- npm: https://www.npmjs.com/package/worker_threads_clusters
- Repository: https://github.com/hackermondev/worker_threads_clusters
- Homepage: https://github.com/hackermondev/worker_threads_clusters#readme
- Issues: https://github.com/hackermondev/worker_threads_clusters/issues
- npm.io page: https://npm.io/package/worker_threads_clusters

## Dependencies (11)

- [cors](https://npm.io/package/cors.md) ^2.8.5
- [axios](https://npm.io/package/axios.md) ^1.1.3
- [chalk](https://npm.io/package/chalk.md) ^4.1.2
- [sitka](https://npm.io/package/sitka.md) ^1.0.5
- [esbuild](https://npm.io/package/esbuild.md) ^0.15.12
- [express](https://npm.io/package/express.md) ^4.18.2
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.7
- [@types/cors](https://npm.io/package/@types/cors.md) ^2.8.12
- [body-parser](https://npm.io/package/body-parser.md) ^1.20.1
- [@types/express](https://npm.io/package/@types/express.md) ^4.17.14
- [@types/node-fetch](https://npm.io/package/@types/node-fetch.md) ^2.6.2

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.0.6 (latest) — 2022-10-25
- 0.0.5 — 2022-10-24
- 0.0.4 — 2022-10-23
- 0.0.3 — 2022-10-23
- 0.0.2 — 2022-10-23
- 0.0.1 — 2022-10-23

## README

Split CPU-intensive tasks on multiple servers (nodes) through node worker_threads.

# Installation
```bash
npm install worker_threads_clusters
```

# What does this do?
Node.js offers a feature called ``worker_threads`` that allows you to launch other node files as threads on the main node program. This can be useful for running programs that might block the event loop or for running CPU-intensive programs.

This package takes that concept one step further by allowing you to spawn workers on different servers (referred to as clusters). This way, you can run CPU-intensive tasks on server clusters and then get the response on the main server without any complicated back-end work.



# Example Usage


### Main Program (the "client")
```js
const { Client } = require('worker_threads_clusters');



// nodeBehavior is how what node should be picked
// 'random' | 'incremental' | 'balancing'
/*
random -  pick a random node
incremental - use the nodes in order, first worker on first node, 2nd worker on 2nd node, etc and it keeps resetting back to first worker and continues the cycle
balancing - it will fetch the cpu(s) usage of all the nodes and choose the best one to spawn the worker based on the usage percentage 
*/

const c = new Client({ nodeBehavior: 'random' });

// connection transport is done through HTTP (udp support coming soon)
c.addNode('http://username:password@node1.clusters.local');
c.addNode('http://username:password@node2.clusters.local');
c.addNode('http://username:password@node3.clusters.local');
c.addNode('http://username:password@node4.clusters.local');
c.addNode('http://username:password@node5.clusters.local');


(async () => {
	// a node is first chosen
	// then the "run.js" file gets bundled with esbuild and uploaded to the node
	// worker will then be launched on node
	
	const worker = await c.spawnWorker('run.js');
	worker.on('online', () => console.log('worker is online'));
	worker.on('error', () => console.log('error'));
	worker.on('exit', () => console.log('exit'));
	worker.on('message', (data) => console.log('recieved message:', data));

	setInterval(()=>{
		worker.postMessage('balls');
	}, 1000)
})();
```


### Node (the "server")
```js
const { Server } = require('worker_threads_clusters');


const s = new Server({
	auth: { 
	    username: 'username',
	    password: 'password' 
    },
    
	port: 80,
	log: true // Whether or not it should console.log when important stufff happens (default = true)
});

s.start();
// the server will automatically start processing requests from clients
```

### Worker (run.js)
```js
const { isMainThread, parentPort } = require('worker_threads');

if(!isMainThread) {
	console.log("yooooo i'm alive!!!!")
	parentPort.on("message", (data) => {
		// messages can be sent and recieved
		console.log('got message:', data);
		parentPort.send(data);
	});

	doSomethingReallyIntensiveCpuTask();
} else {
	console.log("run me through a worker, smh")
}

```

# About
<details>
<summary><strong>Contributing</strong></summary>

Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).

</details>

<details>


<summary><strong>Running Tests</strong></summary>

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

```sh
$ npm install && npm test
```

</details>



# API
Soon

# TODO
This is sort of an unfinished project. Right now, everything mentioned previously is fully implemented and new things are coming soon.

- UDP connection transport
- Finish working on tests
- Docs

# License

Copyright © 2022, [Hackermon](https://github.com/hackermondev).
Released under the [MIT License](LICENSE).

---
_Source: https://npm.io/package/worker_threads_clusters · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
