saltyshiomix / nextron

⚡ Next.js + Electron ⚡
https://npm.im/nextron
MIT License
3.69k stars 215 forks source link

Child process not working in packaged app #427

Closed kentezrabetita closed 2 months ago

kentezrabetita commented 7 months ago

Hello,

I am experiencing an issue related to the execution of a child process in the packaged version of my Nextron application. The child process works as expected in the development environment, but fails to execute when the application is packaged. I am currently testing on an M1 Mac.

The file structure in the main directory is as follows:

main/
├─ helpers/
├─ background.ts
├─ ipc.ts
├─ worker.ts

In my ipc.ts file, I fork a child process from worker.ts:

const child = fork('./main/worker.ts'); // i think the problem also lies here

child.on('message', (message) => {
  console.log('Received message from child:', message);
});

child.on('exit', (code) => {
  console.log(`Child process exited with code ${code}`);
});

export const setupIpcHandlers = () => { // this function is called in the `background.ts`
  ipcMain.on('store-image', async (event, image) => {
    const user: any = state.store.get('user_data');
    const { id } = user;

    if (child.connected) {
      child.send({ image: image, user_id: id });
    }
  });
}

In the worker.ts file, I handle the 'message' event from the main process:

process.on('message', async (message) => {
  // handles a basic HTTP request to upload image
});

In development, the main process successfully sends the message and the child process performs as expected. However, in the packaged app, the child process doesn't seem to be executing.

Despite adding 'exit' and 'error' event handlers to the child process, no error messages or exit codes have been generated.

Upon examining the built version of the app in the ./app directory, I noticed the absence of a worker.js file or other files that i've added. However, references to the worker code exist in background.js. This leads me to suspect that the issue might be related to the way I'm referencing the worker file in const child = fork('./main/worker.ts');.

Any guidance on this issue would be greatly appreciated, particularly on getting the child process to work correctly in the packaged app.

Thank you!

bm777 commented 7 months ago

Hi @kentezrabetita, I managed to include a child process running Python or Rust code via a call from IPC.

I'm about to push it tomorrow or Thursday in another branch, then do a PR to the examples folder of Nextron.

You will be able to use the analogy in .ts, .js easily, but for Python, you need to have Python or Rust installed on the target machine to ruin your app. As I said, if you only use js/ts, no need, it is already packed in Nextron, you just.

kentezrabetita commented 7 months ago

Hey @bm777, that's awesome news! I appreciate your work on this and thanks for the good heads up. Looking forward to your PR!

bm777 commented 6 months ago

@kentezrabetita Here is the example running Python code via IPC on top of the basic lang javascript example.

  • it handles dev mode and pro mode in the background.js
  • Waiting for the merge or review of the PR.

https://github.com/saltyshiomix/nextron/assets/29865600/be3305cb-85a2-4890-a396-5aa0847dedf9

JesTery58 commented 5 months ago

you not passed channel when called child.send()

bm777 commented 5 months ago

@JesTery58 can you be more precise please?

JesTery58 commented 5 months ago

do child.send() function want a channel ?

bm777 commented 5 months ago

@JesTery58 basically when using IPC, I always pass the channel as str, in this case, the channel was : here