I have an Electron app that is launched inside of my main nodejs application. The user has to launch my index.js and then, an Electron window spawn. The problem is that I can't comunicate between these two processes.
I am using spawn because fork doesn't work with Electron. Child.send doesn't work. It does nothing.
In my index.js:
let { spawn } = require("child_process")
let electron = spawn(require("electron"), ["."], {"detached": false, "cwd": "D:\\code\\electron_app", "env": {"some": JSON.stringify(["process", "env"])})
electron.send(JSON.stringify({
message: "some message"
}))
electron.on("close", (code) =>{
process.exit(code)
})
electron.on("exit", (code) => {
process.exit(code)
})
electron.stdout.pipe(process.stdout)
electron.stdio.pipe(process.stdio)
electron.stdin.pipe(process.stdin)
electron.stderr.pipe(process.stderr)
In my main.js of my Electron app:
const {app, BrowserWindow} = require('electron')
let win = null
process.on("message", console.log)
//I haven't put all functions here
This doesn't do anything. The Electron app is launching but the message is not sent. Even no errors. I don't know if there is any other way to do it.
The fact is although NodeJs is spawning the electron process but once new process (electron window process in this case) spawned it's become completely stranger to parent process (NodeJs in this case) until it finishes (success or error) and return something to parent process. So don't relay on IPC.
I'm assuming the fact that as NodeJs is not good for CPU intensive tasks so you want to spawn some CPU intensive task (right now on same server but later on some server less architecture).
Simply just communicate through API end points.
Develop required REST end points in NodeJs and call same in electron window process, if both are on same server, request at localhost:port from child process.
If, above not help, Please share your problem statement on UX level (what/ how you want to show).