Communicate with mainprocess and nodeintegration: false


#1

Hi, I would like to create a BrowserWindow without nodeIntegration.
But at the same time I want to communicate with the main process.
Is there a way?
After looking a bit around does seems not.
But I wonder if it is possible to use socket somehow (or anything else): maybe by spawn’ing a new node instance and then be able to communicate in the following way:
Main process <–> spawn node process (eventually how I can do this?)
Main process -> create BrowserWindow with nodeIntegration: false
BrowserWindow <–> node process
My goal is to be able use the mainprocess feature in a Electron context, and to reuse as much as possible the renderer side in a pure web context.
Than you for your patience.


#2

After looking again at the github issues I found mentioned the ‘preload’ param.
But I’m not sure how it works: will it run on the main process before creating the BrowserWindow
or after this is created so it will run on the renderer process?
In the second case, if I understand correctly, I can access the preload script by the other ones running on the renderer and so I’ll be able to communicate to the main process throught it, right?


#3

I am also curious if it is possible to communicate with main process by launching browser window with node-integration=false through sockets. If you have found anything, please share here.


#4

I ended up using the preload script where I have still access to node and the window object.
So I create the BrowserWindow like so in a main.js:
mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: false,
preload: __dirname + '/preload.js'
}
});
and in the preload.js script I attach what I need on the window object.
const myProperty = { ... };
Object.defineProperty(window, 'myProperty', {
get: function () {
return myProperty;
}
});


Electron app to host external site
Calling ipcRenderer from inside an angular2 application
#5

Thank you for sharing this information. This has resolved my communication issue as well.