josdejong / workerpool

Offload tasks to a pool of workers on node.js and in the browser
Apache License 2.0
2.06k stars 147 forks source link

How to post message from main thread to worker thread ? #398

Closed damartripamungkas closed 1 year ago

damartripamungkas commented 1 year ago

how to post message from main thread to worker thread ? if in node:worker_threads like this, how to in workerpool ?

// childWorker.js
const { parentPort } = require("node:worker_threads")
parentPort.postMessage('my name is damar")
parentPort.on("message", (msg) => {
    console.log("receive message from mainWorker.js :", msg)
})
// mainWorker.js
const { Worker } = require("node:worker_threads")
const { join } = require("node:path")
const initWorker = new Worker(join(__dirname, "./childWorker.js"))
initWorker.on("message", (msg) => {
    console.log("receive message from childWorker.js :", msg)
    initWorker.postMessage("answer from mainWorker :", msg)
})
josdejong commented 1 year ago

There is not an API for that. A feature like that is discussed in #185. I'll close this as a duplicate.