electron/docs/api/ipc-renderer.md

53 lines
1.9 KiB
Markdown
Raw Normal View History

2013-09-09 07:35:57 +00:00
# ipc (renderer)
2013-08-14 22:43:35 +00:00
The `ipc` module provides a few methods so you can send synchronous and
2015-08-27 17:13:25 +00:00
asynchronous messages from the render process (web page) to the main process.
2015-08-28 21:21:37 +00:00
You can also receive replies from the main process.
2015-08-27 00:56:10 +00:00
2015-09-09 21:09:14 +00:00
**Note:** If you want to make use of modules in the main process from the renderer
process, you might consider using the [remote](remote.md) module.
2013-08-14 22:43:35 +00:00
2015-08-27 00:56:10 +00:00
See [ipc (main process)](ipc-main-process.md) for code examples.
## Methods
The `ipc` module has the following methods for sending messages:
2015-09-09 21:09:14 +00:00
**Note:** When using these methods to send a `message` you must also listen
2015-08-27 17:13:25 +00:00
for it in the main process with [`ipc (main process)`](ipc-main-process.md).
2015-08-27 00:56:10 +00:00
2015-08-27 17:13:25 +00:00
### `ipc.send(channel[, arg1][, arg2][, ...])`
2015-08-27 00:56:10 +00:00
* `channel` String - The event name.
2015-08-28 21:57:14 +00:00
* `arg` (optional)
2015-08-27 00:56:10 +00:00
2015-08-27 17:13:25 +00:00
Send an event to the main process asynchronously via a `channel`. Optionally,
there can be a message: one or a series of arguments, `arg`, which can have any
type. The main process handles it by listening for the `channel` event with
`ipc`.
2013-08-14 22:43:35 +00:00
2015-08-27 17:13:25 +00:00
### `ipc.sendSync(channel[, arg1][, arg2][, ...])`
2013-08-14 22:43:35 +00:00
2015-08-27 00:56:10 +00:00
* `channel` String - The event name.
2015-08-28 21:57:14 +00:00
* `arg` (optional)
2013-08-14 22:43:35 +00:00
2015-08-27 17:13:25 +00:00
Send an event to the main process synchronously via a `channel`. Optionally,
there can be a message: one or a series of arguments, `arg`, which can have any
type. The main process handles it by listening for the `channel` event with
`ipc`.
2013-08-14 22:43:35 +00:00
2015-08-27 17:13:25 +00:00
The main process handles it by listening for the `channel` event with `ipc` and
replies by setting the `event.returnValue`.
2013-08-14 22:43:35 +00:00
2015-08-27 00:56:10 +00:00
**Note:** Sending a synchronous message will block the whole renderer process so
using this method is not recommended.
2014-12-17 19:09:11 +00:00
2015-08-27 17:13:25 +00:00
### `ipc.sendToHost(channel[, arg1][, arg2][, ...])`
2014-12-17 19:09:11 +00:00
2015-08-27 00:56:10 +00:00
* `channel` String - The event name.
2015-08-28 21:57:14 +00:00
* `arg` (optional)
2014-12-17 19:09:11 +00:00
2015-08-27 17:13:25 +00:00
Like `ipc.send` but the event will be sent to the host page in a `<webview>`
instead of the main process. Optionally, there can be a message: one or a series
of arguments, `arg`, which can have any type.