I am writing a nodejs script. In that I have created a worker using worker_threads and a BroadcastChannel. I am not able to send message from my main thread to worker threads. However, I am able to send message from Worker to main thread.
Following is my code for main.js
let worker = new Worker('worker.js')
let channel = new BroadcastChannel('testChannel', {
type: 'node',
webWorkerSupport: true
})
channel.postMessage('sending message to worker')
channel.onmessage = message => {
console.log('received message in channel main')
console.log(message)
}
Following is the code in worker.js
let channel = new BroadcastChannel('testChannel', {
type: 'node',
webWorkerSupport: true
})
channel.onmessage = message => {
console.log('received message in channel')
console.log(message)
}
channel.postMessage('from worker')
`