electron-async-ipc v1.1.4
electron-async-ipc
⨠Asynchronous electron ipc invocation mechanism .
šæ Installation
Install with npm:
npm i electron-async-ipc --save
šæ Usage
š It must be imported in the main process's entry file .
ļ¼ Otherwise, rendering processes cannot communicate with each other . ļ¼
import 'electron-async-ipc/main'
Main process
import MainIPC from 'electron-async-ipc/main'
// ... other code ...
Renderer process
If a bundler is used, you can just import the module:
import RendererIPC from 'electron-async-ipc/renderer'
// ... other code ...
šæ Example
šµ 1. MainA ā MainB
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 })
šµ 2. Main ā Renderer
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 })
š± 3. RendererA ā RendererB
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 })
š± 4. Renderer ā Main
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 })