| 
									
										
										
										
											2015-11-10 16:48:24 +08:00
										 |  |  | # ipcMain
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-13 10:18:12 -05:00
										 |  |  | The `ipcMain` module is an instance of the | 
					
						
							|  |  |  | [EventEmitter](https://nodejs.org/api/events.html) 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 16:48:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## Sending Messages
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | It is also possible to send messages from the main process to the renderer | 
					
						
							| 
									
										
										
										
											2016-02-16 11:34:39 +08:00
										 |  |  | process, see [webContents.send][web-contents-send] for more information. | 
					
						
							| 
									
										
										
										
											2015-11-10 16:48:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | * When sending a message, the event name is the `channel`. | 
					
						
							|  |  |  | * To reply a synchronous message, you need to set `event.returnValue`. | 
					
						
							|  |  |  | * To send an asynchronous back to the sender, you can use | 
					
						
							|  |  |  |   `event.sender.send(...)`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | An example of sending and handling messages between the render and main | 
					
						
							|  |  |  | processes: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | // In main process. | 
					
						
							| 
									
										
										
										
											2015-11-12 21:20:09 +08:00
										 |  |  | const ipcMain = require('electron').ipcMain; | 
					
						
							| 
									
										
										
										
											2015-11-10 16:48:24 +08:00
										 |  |  | ipcMain.on('asynchronous-message', function(event, arg) { | 
					
						
							|  |  |  |   console.log(arg);  // prints "ping" | 
					
						
							|  |  |  |   event.sender.send('asynchronous-reply', 'pong'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ipcMain.on('synchronous-message', function(event, arg) { | 
					
						
							|  |  |  |   console.log(arg);  // prints "ping" | 
					
						
							|  |  |  |   event.returnValue = 'pong'; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | // In renderer process (web page). | 
					
						
							| 
									
										
										
										
											2015-11-12 21:20:09 +08:00
										 |  |  | const ipcRenderer = require('electron').ipcRenderer; | 
					
						
							| 
									
										
										
										
											2015-11-10 16:48:24 +08:00
										 |  |  | console.log(ipcRenderer.sendSync('synchronous-message', 'ping')); // prints "pong" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ipcRenderer.on('asynchronous-reply', function(event, arg) { | 
					
						
							|  |  |  |   console.log(arg); // prints "pong" | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | ipcRenderer.send('asynchronous-message', 'ping'); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Listening for Messages
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The `ipcMain` module has the following method to listen for events: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 11:34:39 +08:00
										 |  |  | ### `ipcMain.on(channel, listener)`
 | 
					
						
							| 
									
										
										
										
											2015-11-10 16:48:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 11:34:39 +08:00
										 |  |  | * `channel` String | 
					
						
							|  |  |  | * `listener` Function | 
					
						
							| 
									
										
										
										
											2015-11-10 16:48:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 11:34:39 +08:00
										 |  |  | Listens to `channel`, when a new message arrives `listener` would be called with | 
					
						
							|  |  |  | `listener(event, args...)`. | 
					
						
							| 
									
										
										
										
											2016-01-13 10:18:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 11:34:39 +08:00
										 |  |  | ### `ipcMain.once(channel, listener)`
 | 
					
						
							| 
									
										
										
										
											2016-01-13 10:18:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 11:34:39 +08:00
										 |  |  | * `channel` String | 
					
						
							|  |  |  | * `listener` Function | 
					
						
							| 
									
										
										
										
											2016-01-13 10:18:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 11:34:39 +08: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 10:18:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 11:34:39 +08:00
										 |  |  | ### `ipcMain.removeListener(channel, listener)`
 | 
					
						
							| 
									
										
										
										
											2016-01-13 10:18:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 11:34:39 +08:00
										 |  |  | * `channel` String | 
					
						
							|  |  |  | * `listener` Function | 
					
						
							| 
									
										
										
										
											2016-01-13 10:18:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 11:34:39 +08:00
										 |  |  | Removes the specified `listener` from the listener array for the specified | 
					
						
							|  |  |  | `channel`. | 
					
						
							| 
									
										
										
										
											2016-01-14 09:27:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 11:34:39 +08:00
										 |  |  | ### `ipcMain.removeAllListeners([channel])`
 | 
					
						
							| 
									
										
										
										
											2016-01-13 10:18:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 11:34:39 +08:00
										 |  |  | * `channel` String (optional) | 
					
						
							| 
									
										
										
										
											2016-01-13 10:18:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 11:34:39 +08:00
										 |  |  | Removes all listeners, or those of the specified `channel`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Event object
 | 
					
						
							| 
									
										
										
										
											2015-11-10 16:48:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Returns the `webContents` that sent the message, you can call | 
					
						
							|  |  |  | `event.sender.send` to reply to the asynchronous message, see | 
					
						
							| 
									
										
										
										
											2016-02-16 11:34:39 +08:00
										 |  |  | [webContents.send][web-contents-send] for more information. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [web-contents-send]: web-contents.md#webcontentssendchannel-arg1-arg2- |