Simple multithreaded tasks in NodeJS using TypeScript
npm install @tekuconcept/multithreadedMultithreaded is a lightweight utility for running _multithreaded_ workloads in Node.js using native worker threads, with first-class TypeScript support.
It abstracts away most of the worker-thread boilerplate — entry resolution, messaging, and setup — so you can focus on defining worker logic and exchanging data between threads.
- Simple API for managing Node.js worker threads
- TypeScript-first design with strong typings
- Works with both ESM and CommonJS projects
- Supports ts-node for local development
- Handles worker bootstrapping internally
This module is available through the npm registry.
``sh`
$ npm install @tekuconcept/multithreaded
For basic multi-threading, consider using asyncValue. (Works on all threads)
`tsn
const value = await Multithreaded.asyncValue
// Calculate the fibonacci value at
(n: number) => {
if (n <= 1) return n
let prev = 0
let curr = 1
for (let i = 2; i <= n; i++) {
const next = prev + curr
prev = curr
curr = next
}
return curr
},
{ data: 10 }
).timeout(15_000) // optionally timeout the request
`
A slightly more involved example that spawns a worker, exchanges messages, and finally shuts everything down.
`ts
function createWorker() {
const scope = (ctx: WorkerContext) => {
// Let the main thread know we're ready
ctx.post({ type: 'ready', id: ctx.id })
// Main thread seing if we're sill here
ctx.onMessage((msg: any) => {
if (msg.type === 'ping') ctx.post({
type: 'pong',
n: msg.n,
from: ctx.id
})
})
// doing some work in the background
setInterval(() => process.stdout.write('.'), 250)
}
return Multithreaded.addWorker(randomUUID(), scope)
}
// --------------------------------------------------------
Multithreaded.main(() => {
const w1 = createWorker()
// Log all that our worker sends to us
w1.onMessage((message) => console.log(message))
// Let's send a job to our worker...
w1.post({ type: 'ping', n: 1 })
// Force-quit threads with terminateWorkers():
// (Gracefully quit by sending a "shutdown" message)
setTimeout(() => Multithreaded.terminateWorkers(), 1500)
})
``
See the API doc for a brief overview of functions and types.
For more examples and recipes, have a look through the example docs.
To report an issue or suggest a feature, visit the GitHub repo.