electron/docs/api/ipc-main.md

113 lines
3.3 KiB
Markdown
Raw Normal View History

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-11-23 19:20:56 +00:00
Process: [Main](../glossary.md#main-process)
2016-11-03 17:26:00 +00:00
The `ipcMain` module is an instance of the
[EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter) class. 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.
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`.
* 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.
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')
ipcMain.on('asynchronous-message', (event, arg) => {
2017-11-29 10:58:24 +00:00
console.log(arg) // prints "ping"
event.reply('asynchronous-reply', 'pong')
})
2015-11-10 08:48:24 +00:00
ipcMain.on('synchronous-message', (event, arg) => {
2017-11-29 10:58:24 +00:00
console.log(arg) // prints "ping"
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')
console.log(ipcRenderer.sendSync('synchronous-message', 'ping')) // prints "pong"
2015-11-10 08:48:24 +00:00
ipcRenderer.on('asynchronous-reply', (event, arg) => {
console.log(arg) // prints "pong"
})
ipcRenderer.send('asynchronous-message', 'ping')
2015-11-10 08:48:24 +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
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-02-16 03:34:39 +00:00
### `ipcMain.once(channel, listener)`
2016-02-16 03:34:39 +00:00
* `channel` String
* `listener` Function
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-02-16 03:34:39 +00:00
### `ipcMain.removeListener(channel, listener)`
2016-02-16 03:34:39 +00:00
* `channel` String
* `listener` Function
2016-02-16 03:34:39 +00:00
Removes the specified `listener` from the listener array for the specified
`channel`.
2016-02-16 03:34:39 +00:00
### `ipcMain.removeAllListeners([channel])`
* `channel` String
Removes listeners of the specified `channel`.
2016-02-16 03:34:39 +00:00
## Event object
2015-11-10 08:48:24 +00:00
The `event` object passed to the `callback` has the following methods:
### `event.frameId`
An `Integer` representing the ID of the renderer frame that sent this message.
2015-11-10 08:48:24 +00:00
### `event.returnValue`
Set this to the value to be returned in a synchronous message.
### `event.sender`
Returns the `webContents` that sent the message, you can call
`event.sender.send` to reply to the asynchronous message, see
2016-02-16 03:34:39 +00:00
[webContents.send][web-contents-send] for more information.
[web-contents-send]: web-contents.md#contentssendchannel-arg1-arg2-
### `event.reply`
A function that will send an IPC message to the renderer frane that sent
the original message that you are currently handling. You should use this
method to "reply" to the sent message in order to guaruntee the reply will go
to the correct process and frame.