SimulatedGREG / electron-vue

An Electron & Vue.js quick start boilerplate with vue-cli scaffolding, common Vue plugins, electron-packager/electron-builder, unit/e2e testing, vue-devtools, and webpack.
https://simulatedgreg.gitbooks.io/electron-vue/content/
MIT License
15.48k stars 1.55k forks source link

How to use web workers in this boileplate? #864

Open Kaa10 opened 5 years ago

Kaa10 commented 5 years ago

Hello! I am currently in a project that involves: Electron v4.1.4 Vue v2.6.10 Node v10.11.0 Vuetify v1.5.9 Chromium v69.0.3497.128 V8 v6.9.427.31-electron.0 NPM Packages:

ws v6.1.0 (Websocket) pg v7.4.3 (postgreSQL) workerpool 3.1.2 (Web Workers)

Case study: I need to create a Worker in a separate file or in the same component. "Vue" or ".js" that receives an "asynchronous" function. This function creates a Websocket connection with a file named "server.js" running through "pm2" on the server.

Situation: Test the "workerpool" package on electron within a .vue component and it works perfectly. However I can not pass an external function to the worker.

The following examples:

Example 1 - File - Login.vue

TEST_WORKER: async function () { let workerpool = require ('workerpool'); let pool = workerpool.pool (); function add () { let element for (let index = 0; index <1000000000; index ++) { element = index } return element } try { let x = await pool.exec (add) console.log (x) catch (error) { console.log (error) } }

#############################

Example 2 - Login.vue

TEST_WORKER: async function () { var workerpool = require ('workerpool'); var pool = workerpool.pool ();
try { let x = await pool.exec (this.EXTERNAL_FUNCTION_WORKER()) console.log (x) catch (error) { console.log (error) } }

EXTERNAL_FUNCTION_WORKER: function () { let element = 0 for (let index = 0; index <1000000000; index ++) { element = index } return element }

#######################

Situation 2: I can not reference the Worker path. Example:

var pool = workerpool.pool ('/ workers / worker.js'); The file is in a top level folder and app does not find, error 404 is generated. How do I proceed?

appreciate!

Other example real life!

code_1 worker error

d0peCode commented 5 years ago

This is pretty old topic but I run into same issue. In my case code was only:

const Worker = require('worker_threads');

and error was simply

Error: Cannot find module 'worker_threads'

Version of your local NodeJS doesn't really matter because vue-electron ship you own NodeJS which currently by default is 8.9.3. They added worker to node in version 10.5 so the problem lays here. Also vue-electron ship by default electron in version 2.0.17 and current electron newest version (when I write this post) is 5.0.2.

You can check it by opening dev tools within electron window and calling process.versions in console:

image

At the moment I'm not certain what is most convenient way of updating both (node and electron) and I asked about it in #835 and #855 which are related issues. I'll edit this answer as soon as I get any informations.


So I was wrong with NodeJS. It isn't shipped with electron-vue but with electron itself. And electron-vue use electron in version 2.0.17 therefore it has node v8.9.3 packed.

When you update electron it will automatically also update node. You can do it with command npm install electron@latest but then application can stop displaying anything like in my scenerio. I'll update this comment when I find out why.

Kaa10 commented 5 years ago

Hello BorysTyminski! Thanks for the feedback. What I actually do not know is how to add and use web workers, because Webpack can not find the worker's file. If I happen to be able to solve it, I'll post it here. Thank you!

kikakkz commented 5 years ago

Nodejs module "worker_threads" returning error "Error: The V8 platform used by this instance of Node does not support creating Workers".
Even I use lastest electron with v8 7.6.303.22,I still have such error when call new Worker. Howr can I fix that?

d0peCode commented 5 years ago

@kikakkz @Kaa10 The issue is not related to electron-vue. It is electron bug which I reported here Currently no solution.