Search code examples
javascriptnode.jselectronipc

ipcRenderer instead of remote


So apparently it's a bad practice to use the remote module in Electron and they are planning to kill it. They say the ipcRenderer module should be used instead.

But the ipc thing is a event system.

What if within the preload script I need to get a certain variable from the main process? I cannot listen for the ipc event that gives me that variable because it may come after the script has finished! With the remote module when you call the remote.app.function() that gets you what you want the script is blocked until you get the result, just like with any normal function.


Solution

  • remote is nothing more than a wrapper to ipc. If you can do something in remote, it should be available via ipcRenderer as well.

    What you're looking for is sendSync https://www.electronjs.org/docs/api/ipc-renderer#ipcrenderersendsyncchannel-args allows to wait ipc returns message back. But as noted in remote's deprecation reasoning, this is possible footgun in general and should be used carefully.