Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron - Can my app communicate with the main and renderer processes?

Tags:

electron

I have written a very, very basic electron application - The standard hello world type, where you basically have a HTML file which says "Hello, World" - and that lives in the "app" directory within electron, and then is loaded via main.js when you run the app.

Now, lets say I want to be able to maybe communicate with either of those processes (main, or renderer, preferably both!) from the javascript within my application, can that be done? I can't really find anything online about it - but my main problem might be that I don't really even know what to be searching for in the first place. I am very new to Electron.

like image 562
IWishIWasABarista Avatar asked May 09 '17 00:05

IWishIWasABarista


People also ask

How do you send data from main to renderer in Electron?

Communicate asynchronously from the main process to renderer processes. The ipcMain module is an Event Emitter. When used in the main process, it handles asynchronous and synchronous messages sent from a renderer process (web page). Messages sent from a renderer will be emitted to this module.

What is main process and renderer process in Electron?

Electron uses multi-process architecture to manage the application state and user interface. The main process controls the state of the application while the renderer process controls the user interface. Electron uses GPU process to perform graphically intensive tasks but it's optional.

How do you run background worker processes in an Electron app?

Keeping Electron App in BackgroundListen to the window-all-closed event on app and to not quit the app, but instead hide all the renderer windows and the icon in the dock. Create or simply keep the Tray menu. Recreate a window when the users ask for it from the tray menu.

How does IPC work in Electron?

IPC channels​ In Electron, processes communicate by passing messages through developer-defined "channels" with the ipcMain and ipcRenderer modules. These channels are arbitrary (you can name them anything you want) and bidirectional (you can use the same channel name for both modules).


1 Answers

I suppose 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 to a browser window, and receive it using ipcRenderer. Take this example:

Main process:

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

The BrowserWindow called subWindow:

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

When you want to send data from the browser window to the main process, use remote.app.emit. Receive it using app.on. The same example:

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!");
like image 153
Richard Yan Avatar answered Sep 21 '22 23:09

Richard Yan