IpcMainServiceWorker communicates with service workers
IpcMainServiceWorker is an API for communicating asynchronously from the main process to service workers. It is a subtle variation of IpcMain specifically targeted for communicating with service workers, rather than web frames.
ipcMainServiceWorker.on() listens to channel messages
The on(channel, listener) method listens to a channel and calls the listener function when a new message arrives. The listener receives an IpcMainServiceWorkerEvent and any additional arguments passed with the message.
ipcMainServiceWorker.once() adds one-time listener
The once(channel, listener) method adds a one-time listener function for a channel. The listener is invoked only the next time a message is sent to the channel, after which it is automatically removed.
ipcMainServiceWorker.removeListener() removes specific listener
The removeListener(channel, listener) method removes the specified listener from the listener array for the specified channel.
ipcMainServiceWorker.handle() sets up invoke handler
The handle(channel, listener) method sets up a handler for invoke-able IPC messages on a channel. The listener function receives an IpcMainServiceWorkerInvokeEvent and additional arguments, and can return either a Promise or a value directly.
ipcMainServiceWorker.handleOnce() handles single invoke
The handleOnce(channel, listener) method handles a single invoke-able IPC message on a channel, then removes the listener. It works the same as handle() but only processes one message before being automatically removed.
ipcMainServiceWorker.removeHandler() removes handler
The removeHandler(channel) method removes any handler for the specified channel, if one is present.
ipcMain module purpose and usage
The ipcMain module is an Event Emitter that runs in the main process and handles asynchronous and synchronous messages sent from renderer processes. Messages sent from renderers are emitted to this module. It is also possible to send messages from the main process to renderer processes.
ipcMain event name as channel
When sending a message via ipcMain, the event name is referred to as the channel.
Replying to synchronous messages in ipcMain
To reply to a synchronous message in ipcMain, you need to set event.returnValue.
Asynchronous reply using event.reply in ipcMain
To send an asynchronous message back to the sender in ipcMain, use event.reply(...). This helper method automatically handles messages from frames that aren't the main frame, such as iframes, whereas event.sender.send(...) will always send to the main frame.
ipcMain.on method
ipcMain.on(channel, listener) listens to a channel. When a new message arrives on that channel, the listener is called with listener(event, args...). Parameters: channel (string), listener (Function receiving event [IpcMainEvent] and ...args [any[]]).
ipcMain.off method
ipcMain.off(channel, listener) removes the specified listener from the listener array for the specified channel. Parameters: channel (string), listener (Function receiving event [IpcMainEvent] and ...args [any[]]).
ipcMain.once method
ipcMain.once(channel, listener) 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. Parameters: channel (string), listener (Function receiving event [IpcMainEvent] and ...args [any[]]).
ipcMain.addListener method
ipcMain.addListener(channel, listener) is an alias for ipcMain.on(channel, listener). Parameters: channel (string), listener (Function receiving event [IpcMainEvent] and ...args [any[]]).
ipcMain.removeListener method
ipcMain.removeListener(channel, listener) is an alias for ipcMain.off(channel, listener). Parameters: channel (string), listener (Function receiving ...args [any[]]).
ipcMain.removeAllListeners method
ipcMain.removeAllListeners([channel]) removes all listeners from the specified channel. If no channel is specified, it removes all listeners from all channels. Parameters: channel (string, optional).
ipcMain.handle method for invoke pattern
ipcMain.handle(channel, listener) adds a handler for an invokeable IPC. This handler is called whenever a renderer calls ipcRenderer.invoke(channel, ...args). If the listener returns a Promise, the result of the promise is returned as a reply to the remote caller. Otherwise, the return value of the listener is used as the reply value. Parameters: channel (string), listener (Function<Promise<any> | any> receiving event [IpcMainInvokeEvent] and ...args [any[]]).
ipcMain.handle example with async handler
Example of ipcMain.handle in the main process: ipcMain.handle('my-invokable-ipc', async (event, ...args) => { const result = await somePromise(...args); return result; }). In the renderer process: async () => { const result = await ipcRenderer.invoke('my-invokable-ipc', arg1, arg2); }.
ipcMain.handle error serialization behavior
Errors thrown through ipcMain.handle in the main process are not transparent as they are serialized. Only the message property from the original error is provided to the renderer process.
ipcMain.handleOnce method
ipcMain.handleOnce(channel, listener) handles a single invokeable IPC message, then removes the listener. It works the same as ipcMain.handle(channel, listener) but only for one message. Parameters: channel (string), listener (Function<Promise<any> | any> receiving event [IpcMainInvokeEvent] and ...args [any[]]).
ipcMain.removeHandler method
ipcMain.removeHandler(channel) removes any handler for the specified channel, if present. Parameters: channel (string).
MessagePortMain is the main-process equivalent of DOM MessagePort
MessagePortMain is the main-process-side equivalent of the DOM MessagePort object. It behaves similarly to the DOM version, but uses the Node.js EventEmitter event system instead of the DOM EventTarget system. This means you use port.on('message', ...) to listen for events instead of port.onmessage or port.addEventListener('message', ...).
MessagePortMain inherits from EventEmitter
MessagePortMain is an EventEmitter, which means it uses the Node.js event system for handling messages.
MessagePortMain.postMessage() method signature
The postMessage method takes two parameters: message (any type, required) and transfer (MessagePortMain array, optional). It sends a message from the port and optionally transfers ownership of objects to other browsing contexts.
MessagePortMain.start() enables message sending
The start() method starts the sending of messages queued on the port. Messages will be queued until this method is called.
MessagePortMain.close() disconnects the port
The close() method disconnects the port, so it is no longer active.
MessagePortMain 'message' event structure
The 'message' event is emitted when a MessagePortMain object receives a message. The event returns an object with two properties: data (any type) and ports (MessagePortMain array).
MessagePortMain 'close' event indicates disconnection
The 'close' event is emitted when the remote end of a MessagePortMain object becomes disconnected.
MessagePortMain is only available as a return value
MessagePortMain is not exported from the 'electron' module. It is only available as a return value of other methods in the Electron API.
process.parentPort for UtilityProcess communication
process.parentPort is an Electron.ParentPort property if the process is a UtilityProcess (or null otherwise), allowing communication with the parent process.