56

I'm trying to understand the communications between electrons main and renderer processes. The documentation https://github.com/electron/electron/blob/master/docs/api/remote.md states that the "remote module provides a simple way to do inter-process communication (IPC) between the renderer process and the main process."

However, the documentation is very sparse with regards to how to set it up.

I can get the IPC examples to work with my application which seems simple enough. In what scenarios should the remote module be used ?

user1513388
  • 7,165
  • 14
  • 69
  • 111

1 Answers1

87

From the remote docs:

In Electron, GUI-related modules (such as dialog, menu etc.) are only available in the main process, not in the renderer process. In order to use them from the renderer process, the ipc module is necessary to send inter-process messages to the main process.With the remote module, you can invoke methods of the main process object without explicitly sending inter-process messages, similar to Java's RMI. An example of creating a browser window from a renderer process:

const remote = require('electron').remote;
const BrowserWindow = remote.BrowserWindow;

var win = new BrowserWindow({ width: 800, height: 600 });
win.loadURL('https://github.com');

Basically the remote module makes it easy to do stuff normally restricted to the main process in a render process without lots of manual ipc messages back and forth.

So, in a renderer process, instead of:

const ipc = require('electron').ipcRenderer;
ipc.send('show-dialog', { msg: 'my message' });
ipc.on('dialog-shown', () => { /*do stuff*/ });

(And then code in the main to do stuff in response to those messages).

You can just do this all in the renderer:

const remote = require('electron').remote;
const dialog = remote.require('dialog')

dialog.showErrorBox('My message', 'hi.');

The ipc module is not explicitly required (although it's happening for you behind the scenes). Not to say the two are mutually exclusive.

One further question when using remote. Is it possible to access a function that exists in the main process rather than a module ?

I think what you're really asking is: how can I share code between main/renderer processes and how do I require a module in the renderer?

EDIT: You can just require it like normal. An edge case of this is if your renderer window's current URL isn't pointed to a local file (not loaded using file://). If you're loading a remote URL, your require path needs to be absolute or you can use remote like I said below.

OLD:

This is another use case for remote. For example:

remote.require('./services/PowerMonitor.js')

Note that using remote like that causes your code to be run in the context of the main process. That might have it's uses but I would be careful.

Built-in node modules or electron be required like normal:

require('electron')
require('fs')

Can I access global variables from the renderer?

Yes.

//in main
global.test = 123;
//in renderer
remote.getGlobal('test') //=> 123
ccnokes
  • 6,885
  • 5
  • 47
  • 54
  • One further question when using remote. Is it possible to access a function that exists in the main process rather than a module ? – user1513388 Apr 12 '16 at 09:41
  • Actually the same applies to global variables, is it possible to attach to those from the renderer process too ? – user1513388 Apr 12 '16 at 10:32
  • 1
    @user1513388 I answered your comments in my answer. – ccnokes Apr 12 '16 at 16:12
  • Thanks again. And finally :) is it possible to pass from the renderer process to the main process using remote as oppose to just calling the function, or do I need to use IPC ? I can't seem to get this bit to work. – user1513388 Apr 13 '16 at 22:32
  • Ignore the last comment. The answer is yes. Worked it out. Thanks again for clarifying this. – user1513388 Apr 14 '16 at 12:45
  • Thank you @ccnokes , BTW, I found that the require path is relative to the location of your main module (in my case main.js) and to get the dialog module, use: const { dialog } = remote.require('electron') – Dr.YSG Apr 25 '17 at 18:07
  • 4
    Wow I wish I knew about the power of remote earlier. So much time wasted with ipc! This is one of the most useful things I've ever read on StackOverflow. Thanks a lot for this. – Slbox Apr 28 '17 at 00:48
  • if your using react with a hot code development server, besure to prepend 'window.' before any require(); if not you will get a fs path exception – 1-14x0r Sep 27 '17 at 01:46
  • 1
    Important: Do not try and set a global variable using `remote` in the Renderer process. `remote` creates a copy of the global, it doesn't make a reference to it. See https://stackoverflow.com/a/52167197/293280 for more. – Joshua Pinter Sep 04 '18 at 13:24
  • using remote is not reccomended anymore, see https://medium.com/@nornagon/electrons-remote-module-considered-harmful-70d69500f31 – gorn Dec 01 '20 at 19:51