ipcMain
Process: Main
The ipcMain
module is an . 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.
It is also possible to send messages from the main process to the renderer process, see webContents.send for more information.
- 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) whereasevent.sender.send(...)
will always send to the main frame.
An example of sending and handling messages between the render and main processes:
The ipcMain
module has the following method to listen for events:
channel
stringlistener
Functionevent
IpcMainEvent...args
any[]
channel
stringlistener
Functionevent
IpcMainEvent...args
any[]
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.
channel
string
Removes the specified listener
from the listener array for the specified
channel
.
ipcMain.removeAllListeners([channel])
channel
string (optional)
Removes listeners of the specified channel
.
channel
stringlistener
Function| any> event
IpcMainInvokeEvent- 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.
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
for details.
ipcMain.handleOnce(channel, listener)
channel
stringlistener
Function| any> event
IpcMainInvokeEvent...args
any[]
Handles a single invoke
able IPC message, then removes the listener. See
ipcMain.handle(channel, listener)
.
channel
string
Removes any handler for channel
, if present.
The documentation for the event
object passed to the callback
can be found
in the structure docs.
The documentation for the event
object passed to callbacks can be
found in the ipc-main-invoke-event
structure docs.