ipcMain

Communicate asynchronously from the main process to renderer processes.

Process: Main

The ipcMain module is an Event Emitter. When used in the main process, it handles asynchronous and synchronous messages sent from a renderer process (web page). Messages sent from a renderer will be emitted to this module.

Sending Messages

It is also possible to send messages from the main process to the renderer process, see webContents.send for more information.

  • When sending a message, the event name is the channel.
  • To reply to a synchronous message, you need to set event.returnValue.
  • To send an asynchronous message back to the sender, you can use event.reply(...). This helper method will automatically handle messages coming from frames that aren’t the main frame (e.g. iframes) whereas event.sender.send(...) will always send to the main frame.

An example of sending and handling messages between the render and main processes:

  1. // In main process.
  2. const { ipcMain } = require('electron')
  3. ipcMain.on('asynchronous-message', (event, arg) => {
  4. console.log(arg) // prints "ping"
  5. event.reply('asynchronous-reply', 'pong')
  6. })
  7. ipcMain.on('synchronous-message', (event, arg) => {
  8. console.log(arg) // prints "ping"
  9. event.returnValue = 'pong'
  10. })
  1. // In renderer process (web page).
  2. // NB. Electron APIs are only accessible from preload, unless contextIsolation is disabled.
  3. // See https://www.electronjs.org/docs/tutorial/process-model#preload-scripts for more details.
  4. const { ipcRenderer } = require('electron')
  5. console.log(ipcRenderer.sendSync('synchronous-message', 'ping')) // prints "pong"
  6. ipcRenderer.on('asynchronous-reply', (event, arg) => {
  7. console.log(arg) // prints "pong"
  8. })
  9. ipcRenderer.send('asynchronous-message', 'ping')

Methods

The ipcMain module has the following method to listen for events:

ipcMain.on(channel, listener)

  • channel String
  • listener Function
    • event IpcMainEvent
    • ...args any[]

Listens to channel, when a new message arrives listener would be called with listener(event, args...).

ipcMain.once(channel, listener)

  • channel String
  • listener Function
    • event IpcMainEvent
    • ...args any[]

Adds a one time listener function for the event. This listener is invoked only the next time a message is sent to channel, after which it is removed.

ipcMain.removeListener(channel, listener)

  • channel String
  • listener Function
    • ...args any[]

Removes the specified listener from the listener array for the specified channel.

ipcMain.removeAllListeners([channel])

  • channel String (optional)

Removes listeners of the specified channel.

ipcMain.handle(channel, listener)

  • channel String
  • listener Function<Promise\<void> | any>
    • event IpcMainInvokeEvent
    • ...args any[]

Adds a handler for an invokeable IPC. This handler will be called whenever a renderer calls ipcRenderer.invoke(channel, ...args).

If listener returns a Promise, the eventual result of the promise will be returned as a reply to the remote caller. Otherwise, the return value of the listener will be used as the value of the reply.

  1. // Main process
  2. ipcMain.handle('my-invokable-ipc', async (event, ...args) => {
  3. const result = await somePromise(...args)
  4. return result
  5. })
  6. // Renderer process
  7. async () => {
  8. const result = await ipcRenderer.invoke('my-invokable-ipc', arg1, arg2)
  9. // ...
  10. }

The event that is passed as the first argument to the handler is the same as that passed to a regular event listener. It includes information about which WebContents is the source of the invoke request.

Errors thrown through handle in the main process are not transparent as they are serialized and only the message property from the original error is provided to the renderer process. Please refer to #24427 for details.

ipcMain.handleOnce(channel, listener)

  • channel String
  • listener Function<Promise\<void> | any>
    • event IpcMainInvokeEvent
    • ...args any[]

Handles a single invokeable IPC message, then removes the listener. See ipcMain.handle(channel, listener).

ipcMain.removeHandler(channel)

  • channel String

Removes any handler for channel, if present.

IpcMainEvent object

The documentation for the event object passed to the callback can be found in the ipc-main-event structure docs.

IpcMainInvokeEvent object

The documentation for the event object passed to handle callbacks can be found in the ipc-main-invoke-event structure docs.