ipcMain
An object that carries handle
, invoke
and remove
methods to manage the asynchronous communication from the main to renderer processes.
ipcMain.handle[channel / handler name]
Register the IPC handler for the given channel. The handler will be called whenever the ipcRenderer.invoke
method is called with the same channel.
It can be simply used as the following example:
ipcMain.handle.getPing()
Where getPing
is the name of the channel (handler) registered in the createInterprocess
or createIpcSlice
functions.
Also, you can pass a callback function to handle the request from the renderer process if you need to do something before sending the response:
ipcMain.handle.getPing(async (_, { getPing, data }) => { // call the registered handler if needed const response = await getPing(_, data) await ipcMain.invoke.getPong(mainWindow, 'pong') ipcMain.remove.getPing() return 'The getPong ipc was removed' })
ipcMain.invoke[channel / handler name]
Send a message to the renderer process and wait for the response if needed:
const response = await ipcMain.invoke.getPong(mainWindow, 'pong') console.log(response)
ipcMain.remove[channel / handler name]
Remove the listener from the main process:
ipcMain.remove.getPing()