✨ Asynchronous electron ipc invocation mechanism .
Install with npm:
npm i electron-async-ipc --save
📍 It must be imported in the main process's entry file .
( Otherwise, rendering processes cannot communicate with each other . )
import 'electron-async-ipc/main'
import MainIPC from 'electron-async-ipc/main'
// ... other code ...
If a bundler is used, you can just import the module:
import RendererIPC from 'electron-async-ipc/renderer'
// ... other code ...
The main process can send a message to the main process and receive a callback as shown in the following example :
// MainA:
const res = await MainIPC.invokeMain(`${channel}`, ...args)
// MainB: (two choices)
MainIPC.handleMain(`${channel}`, async (...args) => { return res })
MainIPC.handleMainOnce(`${channel}`, async (...args) => { return res })
The main process can send a message to the rendering process and receive a callback as shown in the following example :
/* MainA: (two choices) */
const res = await MainIPC.invokeRenderer(`${webContents}`, `${channel}`, ...args)
const res = await MainIPC.invokeAllRenderer(`${channel}`, ...args)
/* RendererB: (two choices) */
RendererIPC.handleMain(`${channel}`, async (...args) => { return res })
RendererIPC.handleMainOnce(`${channel}`, async (...args) => { return res })
The rendering process can send a message to the rendering process and receive a callback as shown in the following example :
// RendererA:
const res = await RendererIPC.invokeRenderer(`${channel}`, ...args)
// RendererB: (two choices)
RendererIPC.handleRenderer(`${channel}`, async (...args) => { return res })
RendererIPC.handleRendererOnce(`${channel}`, async (...args) => { return res })
The rendering process can send a message to the main process and receive a callback as shown in the following example :
// RendererA:
const res = await RendererIPC.invokeMain(`${channel}`, ...args)
// MainB:
MainIPC.handleRenderer(`${channel}`, async (...args) => { return res })
The example is the once case ( * handleRendererOnce must be used with invokeMainOnce ) :
// RendererA:
const res = await RendererIPC.invokeMainOnce(`${channel}`, ...args)
// MainB:
MainIPC.handleRendererOnce(`${channel}`, async (...args) => { return res })