Inter-Process Communication

Inter-process communication (IPC) is a key part of building feature-rich desktop applications in Electron. Because the main and renderer processes have different responsibilities in Electron’s process model, IPC is the only way to perform many common tasks, such as calling a native API from your UI or triggering changes in your web contents from native menus.

IPC channels

In Electron, processes communicate by passing messages through developer-defined “channels” with the ipcMain and ipcRenderer modules. These channels are arbitrary (you can name them anything you want) and bidirectional (you can use the same channel name for both modules).

In this guide, we’ll be going over some fundamental IPC patterns with concrete examples that you can use as a reference for your app code.

Understanding context-isolated processes

Before proceeding to implementation details, you should be familiar with the idea of using a preload script to import Node.js and Electron modules in a context-isolated renderer process.

Pattern 1: Renderer to main (one-way)

To fire a one-way IPC message from a renderer process to the main process, you can use the ipcRenderer.send API to send a message that is then received by the ipcMain.on API.

You usually use this pattern to call a main process API from your web contents. We’ll demonstrate this pattern by creating a simple app that can programmatically change its window title.

For this demo, you’ll need to add code to your main process, your renderer process, and a preload script. The full code is below, but we’ll be explaining each file individually in the following sections.

docs/fiddles/ipc/pattern-1 (23.0.0)Open in Fiddle

  • main.js
  • preload.js
  • index.html
  • renderer.js
  1. const {app, BrowserWindow, ipcMain} = require('electron')
  2. const path = require('path')
  3. function createWindow () {
  4. const mainWindow = new BrowserWindow({
  5. webPreferences: {
  6. preload: path.join(__dirname, 'preload.js')
  7. }
  8. })
  9. ipcMain.on('set-title', (event, title) => {
  10. const webContents = event.sender
  11. const win = BrowserWindow.fromWebContents(webContents)
  12. win.setTitle(title)
  13. })
  14. mainWindow.loadFile('index.html')
  15. }
  16. app.whenReady().then(() => {
  17. createWindow()
  18. app.on('activate', function () {
  19. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  20. })
  21. })
  22. app.on('window-all-closed', function () {
  23. if (process.platform !== 'darwin') app.quit()
  24. })
  1. const { contextBridge, ipcRenderer } = require('electron')
  2. contextBridge.exposeInMainWorld('electronAPI', {
  3. setTitle: (title) => ipcRenderer.send('set-title', title)
  4. })
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
  6. <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
  7. <title>Hello World!</title>
  8. </head>
  9. <body>
  10. Title: <input id="title"/>
  11. <button id="btn" type="button">Set</button>
  12. <script src="./renderer.js"></script>
  13. </body>
  14. </html>
  1. const setButton = document.getElementById('btn')
  2. const titleInput = document.getElementById('title')
  3. setButton.addEventListener('click', () => {
  4. const title = titleInput.value
  5. window.electronAPI.setTitle(title)
  6. });

1. Listen for events with ipcMain.on

In the main process, set an IPC listener on the set-title channel with the ipcMain.on API:

main.js (Main Process)

  1. const {app, BrowserWindow, ipcMain} = require('electron')
  2. const path = require('path')
  3. //...
  4. function handleSetTitle (event, title) {
  5. const webContents = event.sender
  6. const win = BrowserWindow.fromWebContents(webContents)
  7. win.setTitle(title)
  8. }
  9. function createWindow () {
  10. const mainWindow = new BrowserWindow({
  11. webPreferences: {
  12. preload: path.join(__dirname, 'preload.js')
  13. }
  14. })
  15. mainWindow.loadFile('index.html')
  16. }
  17. app.whenReady().then(() => {
  18. ipcMain.on('set-title', handleSetTitle)
  19. createWindow()
  20. }
  21. //...

The above handleSetTitle callback has two parameters: an IpcMainEvent structure and a title string. Whenever a message comes through the set-title channel, this function will find the BrowserWindow instance attached to the message sender and use the win.setTitle API on it.

Inter-Process Communication - 图1info

Make sure you’re loading the index.html and preload.js entry points for the following steps!

2. Expose ipcRenderer.send via preload

To send messages to the listener created above, you can use the ipcRenderer.send API. By default, the renderer process has no Node.js or Electron module access. As an app developer, you need to choose which APIs to expose from your preload script using the contextBridge API.

In your preload script, add the following code, which will expose a global window.electronAPI variable to your renderer process.

preload.js (Preload Script)

  1. const { contextBridge, ipcRenderer } = require('electron')
  2. contextBridge.exposeInMainWorld('electronAPI', {
  3. setTitle: (title) => ipcRenderer.send('set-title', title)
  4. })

At this point, you’ll be able to use the window.electronAPI.setTitle() function in the renderer process.

Inter-Process Communication - 图2Security warning

We don’t directly expose the whole ipcRenderer.send API for security reasons. Make sure to limit the renderer’s access to Electron APIs as much as possible.

3. Build the renderer process UI

In our BrowserWindow’s loaded HTML file, add a basic user interface consisting of a text input and a button:

index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
  6. <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
  7. <title>Hello World!</title>
  8. </head>
  9. <body>
  10. Title: <input id="title"/>
  11. <button id="btn" type="button">Set</button>
  12. <script src="./renderer.js"></script>
  13. </body>
  14. </html>

To make these elements interactive, we’ll be adding a few lines of code in the imported renderer.js file that leverages the window.electronAPI functionality exposed from the preload script:

renderer.js (Renderer Process)

  1. const setButton = document.getElementById('btn')
  2. const titleInput = document.getElementById('title')
  3. setButton.addEventListener('click', () => {
  4. const title = titleInput.value
  5. window.electronAPI.setTitle(title)
  6. });

At this point, your demo should be fully functional. Try using the input field and see what happens to your BrowserWindow title!

Pattern 2: Renderer to main (two-way)

A common application for two-way IPC is calling a main process module from your renderer process code and waiting for a result. This can be done by using ipcRenderer.invoke paired with ipcMain.handle.

In the following example, we’ll be opening a native file dialog from the renderer process and returning the selected file’s path.

For this demo, you’ll need to add code to your main process, your renderer process, and a preload script. The full code is below, but we’ll be explaining each file individually in the following sections.

docs/fiddles/ipc/pattern-2 (23.0.0)Open in Fiddle

  • main.js
  • preload.js
  • index.html
  • renderer.js
  1. const {app, BrowserWindow, ipcMain, dialog} = require('electron')
  2. const path = require('path')
  3. async function handleFileOpen() {
  4. const { canceled, filePaths } = await dialog.showOpenDialog()
  5. if (canceled) {
  6. return
  7. } else {
  8. return filePaths[0]
  9. }
  10. }
  11. function createWindow () {
  12. const mainWindow = new BrowserWindow({
  13. webPreferences: {
  14. preload: path.join(__dirname, 'preload.js')
  15. }
  16. })
  17. mainWindow.loadFile('index.html')
  18. }
  19. app.whenReady().then(() => {
  20. ipcMain.handle('dialog:openFile', handleFileOpen)
  21. createWindow()
  22. app.on('activate', function () {
  23. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  24. })
  25. })
  26. app.on('window-all-closed', function () {
  27. if (process.platform !== 'darwin') app.quit()
  28. })
  1. const { contextBridge, ipcRenderer } = require('electron')
  2. contextBridge.exposeInMainWorld('electronAPI',{
  3. openFile: () => ipcRenderer.invoke('dialog:openFile')
  4. })
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
  6. <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
  7. <title>Dialog</title>
  8. </head>
  9. <body>
  10. <button type="button" id="btn">Open a File</button>
  11. File path: <strong id="filePath"></strong>
  12. <script src='./renderer.js'></script>
  13. </body>
  14. </html>
  1. const btn = document.getElementById('btn')
  2. const filePathElement = document.getElementById('filePath')
  3. btn.addEventListener('click', async () => {
  4. const filePath = await window.electronAPI.openFile()
  5. filePathElement.innerText = filePath
  6. })

1. Listen for events with ipcMain.handle

In the main process, we’ll be creating a handleFileOpen() function that calls dialog.showOpenDialog and returns the value of the file path selected by the user. This function is used as a callback whenever an ipcRender.invoke message is sent through the dialog:openFile channel from the renderer process. The return value is then returned as a Promise to the original invoke call.

Inter-Process Communication - 图3A word on error handling

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 #24427 for details.

main.js (Main Process)

  1. const { BrowserWindow, dialog, ipcMain } = require('electron')
  2. const path = require('path')
  3. //...
  4. async function handleFileOpen() {
  5. const { canceled, filePaths } = await dialog.showOpenDialog()
  6. if (canceled) {
  7. return
  8. } else {
  9. return filePaths[0]
  10. }
  11. }
  12. function createWindow () {
  13. const mainWindow = new BrowserWindow({
  14. webPreferences: {
  15. preload: path.join(__dirname, 'preload.js')
  16. }
  17. })
  18. mainWindow.loadFile('index.html')
  19. }
  20. app.whenReady(() => {
  21. ipcMain.handle('dialog:openFile', handleFileOpen)
  22. createWindow()
  23. })
  24. //...

Inter-Process Communication - 图4on channel names

The dialog: prefix on the IPC channel name has no effect on the code. It only serves as a namespace that helps with code readability.

Inter-Process Communication - 图5info

Make sure you’re loading the index.html and preload.js entry points for the following steps!

2. Expose ipcRenderer.invoke via preload

In the preload script, we expose a one-line openFile function that calls and returns the value of ipcRenderer.invoke('dialog:openFile'). We’ll be using this API in the next step to call the native dialog from our renderer’s user interface.

preload.js (Preload Script)

  1. const { contextBridge, ipcRenderer } = require('electron')
  2. contextBridge.exposeInMainWorld('electronAPI', {
  3. openFile: () => ipcRenderer.invoke('dialog:openFile')
  4. })

Inter-Process Communication - 图6Security warning

We don’t directly expose the whole ipcRenderer.invoke API for security reasons. Make sure to limit the renderer’s access to Electron APIs as much as possible.

3. Build the renderer process UI

Finally, let’s build the HTML file that we load into our BrowserWindow.

index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
  6. <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
  7. <title>Dialog</title>
  8. </head>
  9. <body>
  10. <button type="button" id="btn">Open a File</button>
  11. File path: <strong id="filePath"></strong>
  12. <script src='./renderer.js'></script>
  13. </body>
  14. </html>

The UI consists of a single #btn button element that will be used to trigger our preload API, and a #filePath element that will be used to display the path of the selected file. Making these pieces work will take a few lines of code in the renderer process script:

renderer.js (Renderer Process)

  1. const btn = document.getElementById('btn')
  2. const filePathElement = document.getElementById('filePath')
  3. btn.addEventListener('click', async () => {
  4. const filePath = await window.electronAPI.openFile()
  5. filePathElement.innerText = filePath
  6. })

In the above snippet, we listen for clicks on the #btn button, and call our window.electronAPI.openFile() API to activate the native Open File dialog. We then display the selected file path in the #filePath element.

Note: legacy approaches

The ipcRenderer.invoke API was added in Electron 7 as a developer-friendly way to tackle two-way IPC from the renderer process. However, there exist a couple alternative approaches to this IPC pattern.

Inter-Process Communication - 图7Avoid legacy approaches if possible

We recommend using ipcRenderer.invoke whenever possible. The following two-way renderer-to-main patterns are documented for historical purposes.

Inter-Process Communication - 图8info

For the following examples, we’re calling ipcRenderer directly from the preload script to keep the code samples small.

Using ipcRenderer.send

The ipcRenderer.send API that we used for single-way communication can also be leveraged to perform two-way communication. This was the recommended way for asynchronous two-way communication via IPC prior to Electron 7.

preload.js (Preload Script)

  1. // You can also put expose this code to the renderer
  2. // process with the `contextBridge` API
  3. const { ipcRenderer } = require('electron')
  4. ipcRenderer.on('asynchronous-reply', (_event, arg) => {
  5. console.log(arg) // prints "pong" in the DevTools console
  6. })
  7. ipcRenderer.send('asynchronous-message', 'ping')

main.js (Main Process)

  1. ipcMain.on('asynchronous-message', (event, arg) => {
  2. console.log(arg) // prints "ping" in the Node console
  3. // works like `send`, but returning a message back
  4. // to the renderer that sent the original message
  5. event.reply('asynchronous-reply', 'pong')
  6. })

There are a couple downsides to this approach:

  • You need to set up a second ipcRenderer.on listener to handle the response in the renderer process. With invoke, you get the response value returned as a Promise to the original API call.
  • There’s no obvious way to pair the asynchronous-reply message to the original asynchronous-message one. If you have very frequent messages going back and forth through these channels, you would need to add additional app code to track each call and response individually.

Using ipcRenderer.sendSync

The ipcRenderer.sendSync API sends a message to the main process and waits synchronously for a response.

main.js (Main Process)

  1. const { ipcMain } = require('electron')
  2. ipcMain.on('synchronous-message', (event, arg) => {
  3. console.log(arg) // prints "ping" in the Node console
  4. event.returnValue = 'pong'
  5. })

preload.js (Preload Script)

  1. // You can also put expose this code to the renderer
  2. // process with the `contextBridge` API
  3. const { ipcRenderer } = require('electron')
  4. const result = ipcRenderer.sendSync('synchronous-message', 'ping')
  5. console.log(result) // prints "pong" in the DevTools console

The structure of this code is very similar to the invoke model, but we recommend avoiding this API for performance reasons. Its synchronous nature means that it’ll block the renderer process until a reply is received.

Pattern 3: Main to renderer

When sending a message from the main process to a renderer process, you need to specify which renderer is receiving the message. Messages need to be sent to a renderer process via its WebContents instance. This WebContents instance contains a send method that can be used in the same way as ipcRenderer.send.

To demonstrate this pattern, we’ll be building a number counter controlled by the native operating system menu.

For this demo, you’ll need to add code to your main process, your renderer process, and a preload script. The full code is below, but we’ll be explaining each file individually in the following sections.

docs/fiddles/ipc/pattern-3 (23.0.0)Open in Fiddle

  • main.js
  • preload.js
  • index.html
  • renderer.js
  1. const {app, BrowserWindow, Menu, ipcMain} = require('electron')
  2. const path = require('path')
  3. function createWindow () {
  4. const mainWindow = new BrowserWindow({
  5. webPreferences: {
  6. preload: path.join(__dirname, 'preload.js')
  7. }
  8. })
  9. const menu = Menu.buildFromTemplate([
  10. {
  11. label: app.name,
  12. submenu: [
  13. {
  14. click: () => mainWindow.webContents.send('update-counter', 1),
  15. label: 'Increment',
  16. },
  17. {
  18. click: () => mainWindow.webContents.send('update-counter', -1),
  19. label: 'Decrement',
  20. }
  21. ]
  22. }
  23. ])
  24. Menu.setApplicationMenu(menu)
  25. mainWindow.loadFile('index.html')
  26. // Open the DevTools.
  27. mainWindow.webContents.openDevTools()
  28. }
  29. app.whenReady().then(() => {
  30. ipcMain.on('counter-value', (_event, value) => {
  31. console.log(value) // will print value to Node console
  32. })
  33. createWindow()
  34. app.on('activate', function () {
  35. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  36. })
  37. })
  38. app.on('window-all-closed', function () {
  39. if (process.platform !== 'darwin') app.quit()
  40. })
  1. const { contextBridge, ipcRenderer } = require('electron')
  2. contextBridge.exposeInMainWorld('electronAPI', {
  3. handleCounter: (callback) => ipcRenderer.on('update-counter', callback)
  4. })
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
  6. <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
  7. <title>Menu Counter</title>
  8. </head>
  9. <body>
  10. Current value: <strong id="counter">0</strong>
  11. <script src="./renderer.js"></script>
  12. </body>
  13. </html>
  1. const counter = document.getElementById('counter')
  2. window.electronAPI.handleCounter((event, value) => {
  3. const oldValue = Number(counter.innerText)
  4. const newValue = oldValue + value
  5. counter.innerText = newValue
  6. event.sender.send('counter-value', newValue)
  7. })

1. Send messages with the webContents module

For this demo, we’ll need to first build a custom menu in the main process using Electron’s Menu module that uses the webContents.send API to send an IPC message from the main process to the target renderer.

main.js (Main Process)

  1. const {app, BrowserWindow, Menu, ipcMain} = require('electron')
  2. const path = require('path')
  3. function createWindow () {
  4. const mainWindow = new BrowserWindow({
  5. webPreferences: {
  6. preload: path.join(__dirname, 'preload.js')
  7. }
  8. })
  9. const menu = Menu.buildFromTemplate([
  10. {
  11. label: app.name,
  12. submenu: [
  13. {
  14. click: () => mainWindow.webContents.send('update-counter', 1),
  15. label: 'Increment',
  16. },
  17. {
  18. click: () => mainWindow.webContents.send('update-counter', -1),
  19. label: 'Decrement',
  20. }
  21. ]
  22. }
  23. ])
  24. Menu.setApplicationMenu(menu)
  25. mainWindow.loadFile('index.html')
  26. }
  27. //...

For the purposes of the tutorial, it’s important to note that the click handler sends a message (either 1 or -1) to the renderer process through the update-counter channel.

  1. click: () => mainWindow.webContents.send('update-counter', -1)

Inter-Process Communication - 图9info

Make sure you’re loading the index.html and preload.js entry points for the following steps!

2. Expose ipcRenderer.on via preload

Like in the previous renderer-to-main example, we use the contextBridge and ipcRenderer modules in the preload script to expose IPC functionality to the renderer process:

preload.js (Preload Script)

  1. const { contextBridge, ipcRenderer } = require('electron')
  2. contextBridge.exposeInMainWorld('electronAPI', {
  3. onUpdateCounter: (callback) => ipcRenderer.on('update-counter', callback)
  4. })

After loading the preload script, your renderer process should have access to the window.electronAPI.onUpdateCounter() listener function.

Inter-Process Communication - 图10Security warning

We don’t directly expose the whole ipcRenderer.on API for security reasons. Make sure to limit the renderer’s access to Electron APIs as much as possible.

Inter-Process Communication - 图11info

In the case of this minimal example, you can call ipcRenderer.on directly in the preload script rather than exposing it over the context bridge.

preload.js (Preload Script)

  1. const { ipcRenderer } = require('electron')
  2. window.addEventListener('DOMContentLoaded', () => {
  3. const counter = document.getElementById('counter')
  4. ipcRenderer.on('update-counter', (_event, value) => {
  5. const oldValue = Number(counter.innerText)
  6. const newValue = oldValue + value
  7. counter.innerText = newValue
  8. })
  9. })

However, this approach has limited flexibility compared to exposing your preload APIs over the context bridge, since your listener can’t directly interact with your renderer code.

3. Build the renderer process UI

To tie it all together, we’ll create an interface in the loaded HTML file that contains a #counter element that we’ll use to display the values:

index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
  6. <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
  7. <title>Menu Counter</title>
  8. </head>
  9. <body>
  10. Current value: <strong id="counter">0</strong>
  11. <script src="./renderer.js"></script>
  12. </body>
  13. </html>

Finally, to make the values update in the HTML document, we’ll add a few lines of DOM manipulation so that the value of the #counter element is updated whenever we fire an update-counter event.

renderer.js (Renderer Process)

  1. const counter = document.getElementById('counter')
  2. window.electronAPI.onUpdateCounter((_event, value) => {
  3. const oldValue = Number(counter.innerText)
  4. const newValue = oldValue + value
  5. counter.innerText = newValue
  6. })

In the above code, we’re passing in a callback to the window.electronAPI.onUpdateCounter function exposed from our preload script. The second value parameter corresponds to the 1 or -1 we were passing in from the webContents.send call from the native menu.

Optional: returning a reply

There’s no equivalent for ipcRenderer.invoke for main-to-renderer IPC. Instead, you can send a reply back to the main process from within the ipcRenderer.on callback.

We can demonstrate this with slight modifications to the code from the previous example. In the renderer process, use the event parameter to send a reply back to the main process through the counter-value channel.

renderer.js (Renderer Process)

  1. const counter = document.getElementById('counter')
  2. window.electronAPI.onUpdateCounter((event, value) => {
  3. const oldValue = Number(counter.innerText)
  4. const newValue = oldValue + value
  5. counter.innerText = newValue
  6. event.sender.send('counter-value', newValue)
  7. })

In the main process, listen for counter-value events and handle them appropriately.

main.js (Main Process)

  1. //...
  2. ipcMain.on('counter-value', (_event, value) => {
  3. console.log(value) // will print value to Node console
  4. })
  5. //...

Pattern 4: Renderer to renderer

There’s no direct way to send messages between renderer processes in Electron using the ipcMain and ipcRenderer modules. To achieve this, you have two options:

  • Use the main process as a message broker between renderers. This would involve sending a message from one renderer to the main process, which would forward the message to the other renderer.
  • Pass a MessagePort from the main process to both renderers. This will allow direct communication between renderers after the initial setup.

Object serialization

Electron’s IPC implementation uses the HTML standard Structured Clone Algorithm to serialize objects passed between processes, meaning that only certain types of objects can be passed through IPC channels.

In particular, DOM objects (e.g. Element, Location and DOMMatrix), Node.js objects backed by C++ classes (e.g. process.env, some members of Stream), and Electron objects backed by C++ classes (e.g. WebContents, BrowserWindow and WebFrame) are not serializable with Structured Clone.