Electron IPC and API Design
🎯 Introduction
The renderer process handles the page, but cannot directly call app.getVersion() in the main process. To read the application version, the page must send a request to the main process and wait for its response.
This article focuses on one task: displaying the Electron application's version on the page.
By the end of this article, you will be able to:
- Understand where the preload script fits in the communication flow.
- Send requests from the page with
ipcRenderer.invoke. - Handle requests in the main process with
ipcMain.handle. - Display data returned by the main process on the page.
🧱 Why IPC is needed
The application version comes from app.getVersion(), and app is available only in the main process. Since the renderer cannot call main process functions directly, IPC (inter-process communication) carries requests and results between them.
This communication involves three parts:
| Location | Task in this example |
|---|---|
| Renderer process | Request the app version and display the result |
| Preload script | Provide getAppVersion() to the page and forward its request |
| Main process | Call app.getVersion() and return the version to the page |
The complete flow is:
renderer.js
→ window.electronAPI.getAppVersion()
preload.js
→ ipcRenderer.invoke('app:get-version')
main.js
→ ipcMain.handle('app:get-version')
→ Return the version
app:get-version is the channel name for this exchange. The sender and receiver must use the same channel name for messages to match.
🛠 Build the version display demo
The project needs four files:
electron-course/
├── main.js
├── preload.js
├── index.html
└── renderer.js
main.js.1. Handle version requests in the main process
main.js creates the window and handles version requests with ipcMain.handle:
const { app, BrowserWindow, ipcMain } = require('electron/main');
const path = require('node:path');
ipcMain.handle('app:get-version', () => {
return app.getVersion();
});
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
},
});
win.loadFile(path.join(__dirname, 'index.html'));
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
The ipcMain.handle callback runs in the main process, so it can call app.getVersion().
2. Expose a page method through the preload script
preload.js uses contextBridge to expose electronAPI on the page's window:
const { contextBridge, ipcRenderer } = require('electron/renderer');
contextBridge.exposeInMainWorld('electronAPI', {
getAppVersion: () => ipcRenderer.invoke('app:get-version'),
});
The page can then call:
window.electronAPI.getAppVersion();
Internally, this method sends a request to the main process with ipcRenderer.invoke. The page only needs a way to read the version; it does not need access to the rest of Electron's modules.
3. Prepare a place to display the result
Add a paragraph to index.html and load renderer.js:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self'"
/>
<title>Electron IPC</title>
</head>
<body>
<h1>Application information</h1>
<p id="version">Reading version...</p>
<script src="./renderer.js"></script>
</body>
</html>
4. Request the version from the renderer process
renderer.js calls the method exposed by the preload script and displays the returned value:
const versionElement = document.querySelector('#version');
async function showVersion() {
const version = await window.electronAPI.getAppVersion();
versionElement.textContent = `App version: ${version}`;
}
showVersion();
Run the project:
npm start
“Reading version...” changes to something like “App version: 1.0.0”. This value comes from the version field in package.json.
⚡ How invoke and handle work together
This example uses one pair of IPC APIs: invoke and handle.
// preload.js: Send a request and wait for the returned value
ipcRenderer.invoke('app:get-version');
// main.js: Handle requests on the same channel
ipcMain.handle('app:get-version', () => {
return app.getVersion();
});
Here is how the pieces fit together:
| Code | Purpose |
|---|---|
ipcRenderer.invoke(...) | Send a request and return a Promise |
ipcMain.handle(...) | Receive the request and run its handler |
return in the handler | Return the result to invoke |
await | Wait for the main process to return the result |
Remember the core pattern: invoke on the renderer side, handle in the main process, with matching channel names.
🪤 Common mistakes
contextBridge is undefined
This happens when contextBridge is placed in main.js. It belongs in preload.js:
const { contextBridge, ipcRenderer } = require('electron/renderer');
electronAPI is undefined
First check that BrowserWindow specifies the correct preload script:
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
},
Also confirm that preload.js and main.js are in the same directory.
The page keeps showing “Reading version...”
Check that the channel names match exactly on both sides:
// preload.js
ipcRenderer.invoke('app:get-version');
// main.js
ipcMain.handle('app:get-version', () => {});
Even a single different character prevents the main process from receiving the matching request.
🧾 Summary
- The renderer cannot directly call
app.getVersion()in the main process. - The preload script uses
contextBridgeto expose specific methods to the page. ipcRenderer.invokesends a request from the renderer side and returns a Promise.ipcMain.handlereceives requests in the main process and returns results.- Channel names in
invokeandhandlemust match exactly. - Keep main process, preload, and renderer code in their respective files.
❓ Questions and answers
Q1: Why can't renderer.js use app.getVersion() directly?
app belongs to the main process module, while renderer.js runs in the renderer process. The two processes cannot call each other's functions directly.
Q2: Why is preload.js also needed?
It provides a specific entry point for the page. The page calls getAppVersion(), and the preload script forwards the request to the main process.
Q3: Why use await with invoke?
The request travels to another process and waits for a result. invoke therefore returns a Promise; await retrieves its resolved value.
Q4: Is app:get-version a built-in Electron name?
No. It is a channel name we define. You can choose another name, but the sender and receiver must use the same one.
🧪 Practice
Exercise 1: Add an app:get-platform channel so the page can read the current platform name.
ipcMain.handle('app:get-platform', () => {
// Write your code here: return process.platform
});
contextBridge.exposeInMainWorld('electronAPI', {
getAppVersion: () => ipcRenderer.invoke('app:get-version'),
// Write your code here: expose a getPlatform method
});
Exercise 2: Add a #platform element to the page and display the platform name:
async function showPlatform() {
// Write your code here
}
🎉 You have completed a full Electron IPC exchange! You can use the same pattern to request other main process capabilities.
