Electron. Can my application exchange data with main and rendering processes?

I wrote a very, very basic electronic application. The standard type of greeting, in which you have an HTML file that says "Hello, World", and which is located in the "app" directory inside the electron, and then loaded through main.js when the application starts.

Now, let's say I want to be able to communicate with any of these processes (the main or the renderer, preferably both!) From javascript in my application, can this be done? I can’t find anything on the Internet, but my main problem may be that I don’t even know what to look for first. I am very new to Electron.

+4
source share
1 answer

I assume that you are talking about the main process and other browser windows.

You can use BrowserWindow.webContents.send(channel[, arg1][, arg2][, ...])to send messages from the main process to the browser window and receive it using ipcRenderer. Take this example:

The main process:

subWindow.webContents.send("foo","bar");

BrowserWindowcalled subWindow:

var ipc=require("electron").ipcRenderer;
ipc.on("foo",(event, arg1) => {
    console.log(arg1); //Outputs "bar"
});

If you want to send data from a browser window to the main process, use remote.app.emit. Get it with app.on. The same example:

The main process:

var app=require("electron").app;
app.on("test",(arg) => {
    if (arg=="hey!") console.log("ha!");
}

SubWindow:

require("electron").remote.app.emit("test","hey!");
+3
source

Source: https://habr.com/ru/post/1676693/


All Articles