Search code examples
javascriptnode.jsmultithreadingelectronweb-worker

How can I multi-thread in Electron with compatibility for Node.JS and JQuery functions?


I'm programming an Electron application, and a few pages are meant to update a table real-time by fetching Json strings from my web API. The part that matters from this is that my table first has to be loaded into the document (HTML), before or after (it doesn't matter) I start a new JavaScript thread. I already need to use the main process for handling button and table events.

The thread is used for continuously fetching the data, and if it detects differences from the currently loaded document, it'll put those changes back onto the server. Right now, I'm having trouble starting a new thread that has full access to preloaded JS functions that are used in Node, Electron, and JQuery.

Here's a list of what I tried and why it went wrong, so maybe you can help me find a solution to these or suggest a new direction (another NPM module maybe?):

  • Vanilla WebWorkers | Couldn't use any predefined functions from Node, Electron, JQuery because it starts an entirely new process.
  • Node child_process | This builtin function forks a new thread. However, it works in an entirely different environment, meaning that I'm not able to see output if it has any to the console/document.
  • Electron WebWorkers | I found these on the Docs and enabled NodeIntegrationInWorker. However, it had an error that it couldn't find files from workers/api/etc, and I couldn't locate those files either. It's important to note that this wasn't a module that I had to install, so IDK where those files even are on my system after 30mins of searching.
  • There are also a few different NPM modules that I installed to no avail, mostly because they were isolated (like extended WebWorkers similar to Electron WebWorkers) or they were isolated (Extending the child_process().fork function that didn't work either, from Node).
    • node-process
    • workerjs
    • threads
    • webworker-ng
    • webworker-threads

Finally, in an attempt to fix the third option that I mentioned, I tried to focus on the ASAR path, mentioned here. That fixed this error:

Uncaught Error: ENOENT, worker\api\exports\electron.js not found

But now I'm at the point where the thread probably starts once I load the page but doesn't log even a simple "Hello" to the console, probably due to that wonderful thread isolation.

TLDR; I need a way to run a second thread in Electron that has full access to predefined objects. This option should hopefully run behind the main process, not stopping the page from loading once it goes into its infinite while(True) loop.


Solution

  • As answered by Darkrum in the comments:

    setTimeOut or setInterval is your friend.

    After some additional research (comment if I'm wrong here):

    These two functions allow for code to be executed "simultaneously". Because Javascript is technically only a single-threaded language, true multithreading doesn't exist. Instead, the JS engine, when processing setTimeout, will constantly check for updates in the main thread.