2015-11-10 08:48:24 +00:00
|
|
|
# ipcMain
|
|
|
|
|
2016-04-21 22:39:12 +00:00
|
|
|
> Communicate asynchronously from the main process to renderer processes.
|
2016-04-21 22:35:29 +00:00
|
|
|
|
2016-11-23 19:20:56 +00:00
|
|
|
Process: [Main](../glossary.md#main-process)
|
2016-11-03 17:26:00 +00:00
|
|
|
|
2019-07-22 15:20:43 +00:00
|
|
|
The `ipcMain` module is an [Event Emitter][event-emitter]. When used in the main
|
2016-01-13 15:18:12 +00:00
|
|
|
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.
|
2015-11-10 08:48:24 +00:00
|
|
|
|
|
|
|
## Sending Messages
|
|
|
|
|
|
|
|
It is also possible to send messages from the main process to the renderer
|
2016-02-16 03:34:39 +00:00
|
|
|
process, see [webContents.send][web-contents-send] for more information.
|
2015-11-10 08:48:24 +00:00
|
|
|
|
|
|
|
* When sending a message, the event name is the `channel`.
|
2017-05-07 15:06:46 +00:00
|
|
|
* To reply to a synchronous message, you need to set `event.returnValue`.
|
|
|
|
* To send an asynchronous message back to the sender, you can use
|
2019-01-22 19:24:46 +00:00
|
|
|
`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.
|
2015-11-10 08:48:24 +00:00
|
|
|
|
|
|
|
An example of sending and handling messages between the render and main
|
|
|
|
processes:
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
// In main process.
|
2018-09-13 16:10:51 +00:00
|
|
|
const { ipcMain } = require('electron')
|
2016-05-04 17:59:02 +00:00
|
|
|
ipcMain.on('asynchronous-message', (event, arg) => {
|
2017-11-29 10:58:24 +00:00
|
|
|
console.log(arg) // prints "ping"
|
2019-01-22 19:24:46 +00:00
|
|
|
event.reply('asynchronous-reply', 'pong')
|
2016-07-26 01:39:25 +00:00
|
|
|
})
|
2015-11-10 08:48:24 +00:00
|
|
|
|
2016-05-04 17:59:02 +00:00
|
|
|
ipcMain.on('synchronous-message', (event, arg) => {
|
2017-11-29 10:58:24 +00:00
|
|
|
console.log(arg) // prints "ping"
|
2016-07-26 01:39:25 +00:00
|
|
|
event.returnValue = 'pong'
|
|
|
|
})
|
2015-11-10 08:48:24 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
// In renderer process (web page).
|
2018-09-13 16:10:51 +00:00
|
|
|
const { ipcRenderer } = require('electron')
|
2016-07-26 01:39:25 +00:00
|
|
|
console.log(ipcRenderer.sendSync('synchronous-message', 'ping')) // prints "pong"
|
2015-11-10 08:48:24 +00:00
|
|
|
|
2016-05-04 17:59:02 +00:00
|
|
|
ipcRenderer.on('asynchronous-reply', (event, arg) => {
|
2016-07-26 01:39:25 +00:00
|
|
|
console.log(arg) // prints "pong"
|
|
|
|
})
|
|
|
|
ipcRenderer.send('asynchronous-message', 'ping')
|
2015-11-10 08:48:24 +00:00
|
|
|
```
|
|
|
|
|
2016-11-03 03:04:03 +00:00
|
|
|
## Methods
|
2015-11-10 08:48:24 +00:00
|
|
|
|
|
|
|
The `ipcMain` module has the following method to listen for events:
|
|
|
|
|
2016-02-16 03:34:39 +00:00
|
|
|
### `ipcMain.on(channel, listener)`
|
2015-11-10 08:48:24 +00:00
|
|
|
|
2016-02-16 03:34:39 +00:00
|
|
|
* `channel` String
|
|
|
|
* `listener` Function
|
2019-02-19 09:24:19 +00:00
|
|
|
* `event` IpcMainEvent
|
|
|
|
* `...args` any[]
|
2015-11-10 08:48:24 +00:00
|
|
|
|
2016-02-16 03:34:39 +00:00
|
|
|
Listens to `channel`, when a new message arrives `listener` would be called with
|
|
|
|
`listener(event, args...)`.
|
2016-01-13 15:18:12 +00:00
|
|
|
|
2016-02-16 03:34:39 +00:00
|
|
|
### `ipcMain.once(channel, listener)`
|
2016-01-13 15:18:12 +00:00
|
|
|
|
2016-02-16 03:34:39 +00:00
|
|
|
* `channel` String
|
|
|
|
* `listener` Function
|
2019-02-19 09:24:19 +00:00
|
|
|
* `event` IpcMainEvent
|
|
|
|
* `...args` any[]
|
2016-01-13 15:18:12 +00:00
|
|
|
|
2016-02-16 03:34:39 +00:00
|
|
|
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.
|
2016-01-13 15:18:12 +00:00
|
|
|
|
2016-02-16 03:34:39 +00:00
|
|
|
### `ipcMain.removeListener(channel, listener)`
|
2016-01-13 15:18:12 +00:00
|
|
|
|
2016-02-16 03:34:39 +00:00
|
|
|
* `channel` String
|
|
|
|
* `listener` Function
|
2019-10-29 04:18:39 +00:00
|
|
|
* `...args` any[]
|
2016-01-13 15:18:12 +00:00
|
|
|
|
2016-02-16 03:34:39 +00:00
|
|
|
Removes the specified `listener` from the listener array for the specified
|
|
|
|
`channel`.
|
2016-01-14 14:27:24 +00:00
|
|
|
|
2016-02-16 03:34:39 +00:00
|
|
|
### `ipcMain.removeAllListeners([channel])`
|
2016-01-13 15:18:12 +00:00
|
|
|
|
2019-06-04 21:03:24 +00:00
|
|
|
* `channel` String (optional)
|
2016-01-13 15:18:12 +00:00
|
|
|
|
2017-05-24 19:01:58 +00:00
|
|
|
Removes listeners of the specified `channel`.
|
2016-02-16 03:34:39 +00:00
|
|
|
|
2019-05-31 17:25:19 +00:00
|
|
|
### `ipcMain.handle(channel, listener)`
|
|
|
|
|
|
|
|
* `channel` String
|
2020-11-09 21:52:06 +00:00
|
|
|
* `listener` Function<Promise\<void> | any>
|
2019-05-31 17:25:19 +00:00
|
|
|
* `event` IpcMainInvokeEvent
|
|
|
|
* `...args` any[]
|
|
|
|
|
|
|
|
Adds a handler for an `invoke`able 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.
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Main process
|
|
|
|
ipcMain.handle('my-invokable-ipc', async (event, ...args) => {
|
|
|
|
const result = await somePromise(...args)
|
|
|
|
return result
|
|
|
|
})
|
|
|
|
|
|
|
|
// Renderer process
|
|
|
|
async () => {
|
|
|
|
const result = await ipcRenderer.invoke('my-invokable-ipc', arg1, arg2)
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2021-02-02 12:22:20 +00:00
|
|
|
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](https://github.com/electron/electron/issues/24427) for details.
|
|
|
|
|
2019-05-31 17:25:19 +00:00
|
|
|
### `ipcMain.handleOnce(channel, listener)`
|
|
|
|
|
|
|
|
* `channel` String
|
2020-11-09 21:52:06 +00:00
|
|
|
* `listener` Function<Promise\<void> | any>
|
2019-05-31 17:25:19 +00:00
|
|
|
* `event` IpcMainInvokeEvent
|
|
|
|
* `...args` any[]
|
|
|
|
|
|
|
|
Handles a single `invoke`able 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
|
2015-11-10 08:48:24 +00:00
|
|
|
|
2019-02-19 09:24:19 +00:00
|
|
|
The documentation for the `event` object passed to the `callback` can be found
|
|
|
|
in the [`ipc-main-event`](structures/ipc-main-event.md) structure docs.
|
2019-05-31 17:25:19 +00:00
|
|
|
|
|
|
|
## IpcMainInvokeEvent object
|
|
|
|
|
|
|
|
The documentation for the `event` object passed to `handle` callbacks can be
|
|
|
|
found in the [`ipc-main-invoke-event`](structures/ipc-main-invoke-event.md)
|
|
|
|
structure docs.
|
2019-07-22 15:20:43 +00:00
|
|
|
|
|
|
|
[event-emitter]: https://nodejs.org/api/events.html#events_class_eventemitter
|
2020-07-21 05:47:10 +00:00
|
|
|
[web-contents-send]: web-contents.md#contentssendchannel-args
|