Using node worker_threads on server

I’m looking to use worker threads to do some batch processing and still have the main node thread be responsive so it does not block the main thread. I was wondering if there are any hints on how to do this with the latest node version. I read the docs on web_workers. The entry point requires specifying a .js file to run. Meteor does a bunch of packaging for the source files. Is there any file entry point I can use to access server side code so I can run some functions as worker_threads?

All the articles I read were dated (pre web_worker support in node)

1 Like

It’s the same issue with service workers. Meteor doesn’t support having the multiple entry points this would require unfortunately. But you could create a worker as a standalone in the private/ directory and import it there.

1 Like

If anyone is looking for this:


const { Worker } = require('worker_threads');
const fs = require('fs');
const path = require('path');

// Read the worker script & write this string to a temporary JS file
const workerScript = Assets.getText('worker.js'); //pulled from private
const tmpWorker = path.join(process.cwd(), 'worker.js');
      fs.writeFileSync(tmpWorker, workerScript); //write
const worker = new Worker(tmpWorker); // Spawn the worker thread
1 Like

Thanks for this! It works really well in general. Problem I’ve ran into is when the thread wants to use a third party module. It works in development mode but on a built node app it no longer works because of the custom module resolution that Meteor does.

Have you ran into that before?