comlink icon indicating copy to clipboard operation
comlink copied to clipboard

Worker call to main ("the other way around") doesn't work in node?

Open kurt1288 opened this issue 4 years ago • 3 comments

I've tried to implement the solution in https://github.com/GoogleChromeLabs/comlink/issues/506#issuecomment-753457080, but that doesn't seem to work in node.

Because nodejs doesn't have a "self" context, I've tried using "globalThis" instead, but when I run it it still throws a "self is not defined" error (from the expose function in comlink).

In my main.ts file I've tried:

const worker = new Worker(filename);
const obj = Comlink.wrap(nodeEndpoint(worker)) as unknown as Engine;

Comlink.expose(this.engine, nodeEndpoint(worker));

and in my worker file I have tried:

Comlink.wrap(globalThis) as unknown as Engine;

I suspect the issue is with Comlink.expose(this.engine, nodeEndpoint(worker)); and the nodeEndpoint part but I can't figure it out.

kurt1288 avatar Aug 12 '21 04:08 kurt1288

Comlink.wrap(globalThis) is most likely not correct here. That should probably be Comlink.wrap(nodeEndpoint(require('worker_threads').parentPort)).

If that doesn’t work, can you put your example in a gist or something?

surma avatar Aug 12 '21 09:08 surma

Gist: https://gist.github.com/kurt1288/88036897d5cefffc470b3418d0119061. That's a basic example of how I'm trying to use it.

Explanation for the worker.js file (instead of just calling new Worker('./engine.js')). Short version is "because of webpack". Longer version: My webpack configuration bundles both the main and engine files together. Since new Worker() needs a file and not just a class object, I created the worker.js file.

kurt1288 avatar Aug 12 '21 23:08 kurt1288

In your webpack you can define extra entry points, this will create separate files based on the key:

  const entry = {
    polyfill: ["core-js/stable", "regenerator-runtime/runtime"],
    functions: "./src/functions/functions.ts",
    helpers: "./src/functions/helpers.ts",
  };

This results in functions.js and helpers.js in your dist folder. In webpack 5 you can also do just const worker = wrap<WorkerService>(new Worker(new URL("./worker.service.ts", import.meta.url))); which will link/import correctly without any other fussing.

celluj34 avatar Apr 29 '22 18:04 celluj34