Skip to content
Back to skills

Electron Ipc

ASecurity

World Pulse: When adding communication between the Electron main process (`src/main/main.ts`) and the renderer (`src/renderer/`), or modifying the preload script.

  • 30 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added November 8, 2026
developmentrustgoshellnodeexpressapibackendsecurity

Works with

  • api

Security analysis

A100/100

Scanned November 8, 2026

npx -y skills add majiayu000/claude-skill-registry-data --skill electron-ipc --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Electron Ipc?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Electron Ipc
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/majiayu000-electron-ipc/badge)](https://www.skillsdirectory.com/skills/majiayu000-electron-ipc)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: electron-ipc
description: "World Pulse: When adding communication between the Electron main process (`src/main/main.ts`) and the renderer (`src/renderer/`), or modifying the preload script."
---

# Electron IPC

**When to use:** When adding communication between the Electron main process (`src/main/main.ts`) and the renderer (`src/renderer/`), or modifying the preload script.

---

## Architecture (from `src/main/main.ts`)

World Pulse uses Electron with an **embedded backend** launched via Node `fork()`:

```
main process (Electron)
  └── BrowserWindow (renderer)
        └── preload.ts (context bridge)
  └── child_process.fork() → server/index.ts (Express + Socket.io)
```

The renderer communicates with the backend through **Socket.io over HTTP** — not through IPC. IPC is reserved for system-level operations (window management, native dialogs, file access).

---

## Security Baseline (enforced in `main.ts`)

```ts
new BrowserWindow({
  webPreferences: {
    preload: path.join(__dirname, '../preload/index.js'),
    contextIsolation: true,   // REQUIRED — never set to false
    nodeIntegration: false,   // REQUIRED — never set to true
  },
});
```

These two settings are **non-negotiable**. Any PR that weakens them must be rejected.

---

## Preload Pattern (`src/main/preload.ts`)

The preload script is the only place Node.js APIs are exposed to the renderer. Use `contextBridge`:

```ts
import { contextBridge, ipcRenderer } from 'electron';

contextBridge.exposeInMainWorld('electron', {
  // Whitelist only specific, typed methods
  getVersion: () => ipcRenderer.invoke('get-version'),
  openExternal: (url: string) => ipcRenderer.invoke('open-external', url),
});
```

Rules:
- Expose **only** the methods the renderer actually needs.
- Validate and sanitize all arguments before passing to `ipcRenderer`.
- Never expose the raw `ipcRenderer` object.
- Never use `ipcRenderer.sendSync` — it blocks the main process.

---

## Main Process IPC Handlers

```ts
import { ipcMain, shell } from 'electron';

ipcMain.handle('get-version', () => app.getVersion());

ipcMain.handle('open-external', (_event, url: string) => {
  // Validate URL before opening — never trust renderer input
  if (url.startsWith('https://')) {
    return shell.openExternal(url);
  }
});
```

Always use `ipcMain.handle` (async) over `ipcMain.on` for two-way communication.

---

## External Link Handling

External links in the renderer open in the system browser, not Electron:

```ts
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
  shell.openExternal(url);
  return { action: 'deny' };
});
```

This is already wired in `main.ts`. Do not add `<a target="_blank">` without this handler.

---

## Backend Lifecycle

The server is started via `fork()` before the window is created:

```ts
function startBackendServer(): void {
  serverProcess = fork(serverEntry, [], {
    env: { ...process.env, PORT: String(SERVER_PORT) },
    execArgv: is.dev ? ['--import', 'tsx'] : [],
    stdio: 'pipe',
  });
}

app.whenReady().then(() => {
  startBackendServer();
  createWindow();
});
```

On quit, `serverProcess.kill()` must be called to avoid orphaned processes. The `app.on('before-quit')` hook handles this.

---

## Anti-Patterns

- **Never** set `nodeIntegration: true` or `contextIsolation: false`.
- **Never** use `remote` module — it was deprecated and removed.
- **Never** load remote URLs in BrowserWindow without `will-navigate` validation.
- **Never** pass unsanitized renderer input directly to `shell.openExternal`.
- **Never** use `require()` in the renderer — use the context bridge.
- Do not add `webSecurity: false` even in development.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.