2015-03-26 15:20:31 +00:00
|
|
|
# ipc (main process)
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2015-08-28 21:21:37 +00:00
|
|
|
The `ipc` module, when used in the main process, handles asynchronous and
|
|
|
|
synchronous messages sent from a renderer process (web page). Messages sent from
|
|
|
|
a renderer will be emitted to this module.
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2015-08-27 00:27:17 +00:00
|
|
|
## Sending Messages
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2015-08-27 00:27:17 +00:00
|
|
|
It is also possible to send messages from the main process to the renderer
|
|
|
|
process, see [WebContents.send](browser-window.md#webcontentssendchannel-args)
|
|
|
|
for more information.
|
2014-05-07 01:45:39 +00:00
|
|
|
|
2015-08-27 00:27:17 +00:00
|
|
|
- When sending a message, the event name is the `channel`.
|
|
|
|
- To reply a synchronous message, you need to set `event.returnValue`.
|
2015-08-28 21:21:37 +00:00
|
|
|
- To send an asynchronous back to the sender, you can use
|
|
|
|
`event.sender.send(...)`.
|
2015-08-27 00:27:17 +00:00
|
|
|
|
|
|
|
An example of sending and handling messages between the render and main
|
|
|
|
processes:
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2014-04-25 09:35:36 +00:00
|
|
|
```javascript
|
2015-03-26 15:20:31 +00:00
|
|
|
// In main process.
|
2014-04-25 09:35:36 +00:00
|
|
|
var ipc = require('ipc');
|
|
|
|
ipc.on('asynchronous-message', function(event, arg) {
|
|
|
|
console.log(arg); // prints "ping"
|
|
|
|
event.sender.send('asynchronous-reply', 'pong');
|
|
|
|
});
|
|
|
|
|
|
|
|
ipc.on('synchronous-message', function(event, arg) {
|
|
|
|
console.log(arg); // prints "ping"
|
2014-05-10 09:47:54 +00:00
|
|
|
event.returnValue = 'pong';
|
2014-04-25 09:35:36 +00:00
|
|
|
});
|
|
|
|
```
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2014-04-25 09:35:36 +00:00
|
|
|
```javascript
|
2015-03-26 15:20:31 +00:00
|
|
|
// In renderer process (web page).
|
2014-04-25 09:35:36 +00:00
|
|
|
var ipc = require('ipc');
|
|
|
|
console.log(ipc.sendSync('synchronous-message', 'ping')); // prints "pong"
|
|
|
|
|
|
|
|
ipc.on('asynchronous-reply', function(arg) {
|
|
|
|
console.log(arg); // prints "pong"
|
|
|
|
});
|
|
|
|
ipc.send('asynchronous-message', 'ping');
|
|
|
|
```
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2015-08-28 21:21:37 +00:00
|
|
|
## Listening for Messages
|
2015-08-27 00:27:17 +00:00
|
|
|
|
2015-08-28 21:21:37 +00:00
|
|
|
The `ipc` module has the following method to listen for events:
|
2015-08-27 00:27:17 +00:00
|
|
|
|
|
|
|
### `ipc.on(channel, callback)`
|
|
|
|
|
|
|
|
* `channel` String - The event name.
|
|
|
|
* `callback` Function
|
|
|
|
|
|
|
|
When the event occurs the `callback` is called with an `event` object and a
|
|
|
|
message, `arg`.
|
|
|
|
|
|
|
|
## IPC Events
|
|
|
|
|
|
|
|
The `event` object passed to the `callback` has the following methods:
|
|
|
|
|
|
|
|
### `Event.returnValue`
|
|
|
|
|
|
|
|
Set this to the value to be returned in a synchronous message.
|
|
|
|
|
|
|
|
### `Event.sender`
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2015-08-27 00:27:17 +00:00
|
|
|
Returns the `WebContents` that sent the message.
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2015-08-27 17:13:25 +00:00
|
|
|
### `Event.sender.send(channel[, arg1][, arg2][, ...])`
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2015-08-27 00:27:17 +00:00
|
|
|
* `channel` String - The event name.
|
2015-08-27 17:13:25 +00:00
|
|
|
* `arg` (optional)
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2015-08-27 17:13:25 +00:00
|
|
|
This sends an asynchronous message back to the render process. Optionally, there
|
|
|
|
can be one or a series of arguments, `arg`, which can have any type.
|