Inter-process communication IPC in Electron application

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_website-b2b-advance_0.png
    B2B ADVANCE company website development
    1212
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    852
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_bitrix-bitrix-24-1c_fixper_448_0.png
    Website development for FIXPER company
    815

Implementing Inter-Process Communication (IPC) in Electron

IPC in Electron is the only safe way to give renderer process access to system resources. Renderer runs in isolated Chromium context; everything related to file system, native APIs, and Node.js — only through IPC and preload script.

Architecture of Secure IPC

Renderer (Chromium)
    ↓ window.electronAPI.someMethod()
Preload Script (contextBridge)
    ↓ ipcRenderer.invoke('channel', payload)
Main Process (Node.js)
    ↓ ipcMain.handle('channel', handler)

Preload is the security boundary. It exposes to renderer exactly what API is needed, nothing extra.

Preload: Typed Bridge

// main/preload.ts
import { contextBridge, ipcRenderer } from 'electron';

type FileInfo = { path: string; content: string; size: number };

const api = {
  fs: {
    readFile: (path: string): Promise<FileInfo> =>
      ipcRenderer.invoke('fs:readFile', path),
    writeFile: (path: string, content: string): Promise<void> =>
      ipcRenderer.invoke('fs:writeFile', path, content),
  },

  app: {
    getVersion: (): Promise<string> =>
      ipcRenderer.invoke('app:getVersion'),
  },

  window: {
    minimize: () => ipcRenderer.send('window:minimize'),
    maximize: () => ipcRenderer.send('window:maximize'),
    close: () => ipcRenderer.send('window:close'),
  }
};

contextBridge.exposeInMainWorld('electronAPI', api);
export type ElectronAPI = typeof api;

Main: IPC Handlers

// main/ipc-handlers.js
const { ipcMain, app, dialog } = require('electron');
const fs = require('fs/promises');
const path = require('path');

function registerHandlers() {
  // handle — for invoke (with response)
  ipcMain.handle('fs:readFile', async (event, filePath) => {
    const resolvedPath = path.resolve(filePath);
    const allowedDirs = [
      app.getPath('documents'),
      app.getPath('downloads'),
      app.getPath('userData')
    ];

    const isAllowed = allowedDirs.some(dir => resolvedPath.startsWith(dir));
    if (!isAllowed) {
      throw new Error(`Access denied: ${resolvedPath}`);
    }

    const content = await fs.readFile(resolvedPath, 'utf-8');
    const stat = await fs.stat(resolvedPath);
    return { path: resolvedPath, content, size: stat.size };
  });

  ipcMain.handle('fs:writeFile', async (event, filePath, content) => {
    await fs.writeFile(filePath, content, 'utf-8');
  });

  ipcMain.handle('app:getVersion', () => app.getVersion());

  // on — for send (without response)
  ipcMain.on('window:minimize', (event) => {
    BrowserWindow.fromWebContents(event.sender)?.minimize();
  });
}

module.exports = { registerHandlers };

Streaming Data via IPC

For large data volumes:

// main/ipc-handlers.js
ipcMain.handle('fs:readLargeFile', async (event, filePath) => {
  const { port1, port2 } = new MessageChannelMain();
  event.sender.postMessage('port', null, [port1]);

  const stream = require('fs').createReadStream(filePath);
  stream.on('data', (chunk) => {
    port2.postMessage({ type: 'chunk', data: chunk });
  });

  stream.on('end', () => {
    port2.postMessage({ type: 'end' });
    port2.close();
  });
});

Broadcast from Main to All Windows

// main/broadcast.js
const { BrowserWindow } = require('electron');

function broadcast(channel, data) {
  BrowserWindow.getAllWindows().forEach(win => {
    if (!win.isDestroyed()) {
      win.webContents.send(channel, data);
    }
  });
}

module.exports = { broadcast };

Common errors: forgetting error handling in ipcMain.handle, passing non-serializable objects through IPC, not validating paths in handlers for path traversal attacks.