src/skills/desktop-ipc-electron/SKILL.md
Type-safe Electron IPC patterns with typed channels, electron-trpc, MessagePort, and utility process communication
npx skillsauth add agents-inc/skills desktop-ipc-electronInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Quick Guide: All Electron IPC flows through a preload script using
contextBridge.exposeInMainWorld(). Make it type-safe by defining a shared channel map that constrains channel names, payloads, and return types across main, preload, and renderer. For end-to-end type safety with minimal boilerplate, useelectron-trpc(tRPC over IPC). For high-throughput streaming or renderer-to-renderer communication, useMessageChannelMain/MessagePort. For CPU-intensive background work, useutilityProcesswithparentPort. Always validate IPC input in the main process -- treat renderer messages as untrusted.
<critical_requirements>
All code must follow project conventions in CLAUDE.md (kebab-case, named exports, import ordering,
import type, named constants)
(You MUST validate and sanitize ALL data received via IPC in the main process -- treat renderer messages as untrusted input)
(You MUST use contextBridge.exposeInMainWorld() in preload scripts -- never expose ipcRenderer directly)
(You MUST define IPC channel names and payload types in a single shared file -- never use untyped string literals for channel names)
(You MUST use ipcMain.handle() / ipcRenderer.invoke() for request-response IPC -- sendSync blocks the renderer)
(You MUST clean up IPC listeners when components unmount or windows close -- listener leaks cause memory issues and duplicate handlers)
</critical_requirements>
Auto-detection: Electron IPC, ipcMain, ipcRenderer, contextBridge, preload, type-safe IPC, electron-trpc, ipcLink, createIPCHandler, exposeElectronTRPC, MessageChannelMain, MessagePortMain, MessagePort, utilityProcess, parentPort, typed channels, IPC channel map, postMessage, webContents.send, ipcMain.handle, ipcRenderer.invoke
When to use:
When NOT to use:
Key patterns covered:
handle/invoke) with typed wrapperson/send) with typed channelswebContents.send) with typed eventsparentPort and MessagePort transferDetailed Resources:
Electron IPC is stringly typed by default -- channel names are plain strings, payloads are any, and there is no compile-time guarantee that the main process handler matches what the renderer sends. Type-safe IPC solves this by defining a single source of truth for channel names, argument types, and return types, then threading those types through typed wrapper functions.
Three levels of type safety, pick one:
IpcChannelMap interface, create thin typed wrappers around ipcMain/ipcRenderer. Zero dependencies, full control.When to use each:
handle/invoke, send/on, and webContents.send.When NOT to use type-safe IPC:
Define all channel names, argument types, and return types in a single shared file. Both main and renderer import from this file.
// shared/ipc-channels.ts
export interface IpcHandleChannels {
"file:read": (filePath: string) => { content: string };
"file:write": (filePath: string, content: string) => { success: boolean };
"dialog:open": (options: OpenDialogOptions) => string | null;
"app:version": () => string;
}
export interface IpcSendChannels {
"analytics:track": [eventName: string, metadata: Record<string, unknown>];
"log:error": [message: string, stack?: string];
}
export interface IpcMainToRendererChannels {
"update:progress": { percent: number; message: string };
"update:available": { version: string };
"theme:changed": "light" | "dark";
}
Why good: Single source of truth for all IPC contracts, TypeScript catches mismatches at compile time, channel names are autocompleted
See examples/core.md for typed wrappers that consume this map.
Build a typed preload API from the channel map, then augment window so the renderer gets full autocompletion.
// preload.ts
import { contextBridge, ipcRenderer } from "electron";
import type {
IpcHandleChannels,
IpcSendChannels,
} from "../shared/ipc-channels";
type ElectronAPI = {
invoke: <C extends keyof IpcHandleChannels>(
channel: C,
...args: Parameters<IpcHandleChannels[C]>
) => Promise<ReturnType<IpcHandleChannels[C]>>;
send: <C extends keyof IpcSendChannels>(
channel: C,
...args: IpcSendChannels[C]
) => void;
on: (channel: string, callback: (...args: unknown[]) => void) => () => void;
};
contextBridge.exposeInMainWorld("electronAPI", {
invoke: (channel, ...args) => ipcRenderer.invoke(channel, ...args),
send: (channel, ...args) => ipcRenderer.send(channel, ...args),
on: (channel, callback) => {
const listener = (_event: unknown, ...args: unknown[]) => callback(...args);
ipcRenderer.on(channel, listener);
return () => ipcRenderer.removeListener(channel, listener);
},
} satisfies ElectronAPI);
// shared/electron-api.d.ts -- augment window for renderer autocompletion
import type { ElectronAPI } from "../preload";
declare global {
interface Window {
electronAPI: ElectronAPI;
}
}
Why good: renderer gets autocomplete on channel names and typed payloads, on returns an unsubscribe function for easy cleanup
See examples/core.md for the full pattern with main process typed handlers.
For apps with many IPC endpoints, electron-trpc provides the best developer experience by leveraging tRPC's router pattern.
// main/router.ts
import { initTRPC } from "@trpc/server";
import { z } from "zod";
const t = initTRPC.create({ isServer: true });
export const router = t.router({
readFile: t.procedure
.input(z.object({ path: z.string() }))
.query(async ({ input }) => {
const content = await fs.readFile(input.path, "utf-8");
return { content };
}),
saveSettings: t.procedure
.input(z.object({ theme: z.enum(["light", "dark"]) }))
.mutation(async ({ input }) => {
await saveToStore(input);
return { success: true };
}),
});
export type AppRouter = typeof router;
// renderer/client.ts
import { createTRPCProxyClient } from "@trpc/client";
import { ipcLink } from "electron-trpc/renderer";
import type { AppRouter } from "../main/router";
export const trpc = createTRPCProxyClient<AppRouter>({
links: [ipcLink()],
});
// Fully typed -- autocomplete on procedures, typed input/output
const result = await trpc.readFile.query({ path: "/some/file.txt" });
Why good: Zod validates input at runtime in main, TypeScript validates at compile time in renderer, adding a new procedure auto-surfaces in the client
See examples/electron-trpc.md for full setup including preload, subscriptions, and context patterns.
Always validate arguments in main process handlers. The renderer can be compromised via XSS -- main process handlers have full Node.js access.
// main/handlers.ts
const ALLOWED_EXTENSIONS = new Set([".txt", ".md", ".json"]);
const MAX_CONTENT_LENGTH = 10 * 1024 * 1024; // 10MB
ipcMain.handle("file:read", async (_event, filePath: unknown) => {
// Type check
if (typeof filePath !== "string") {
throw new Error("filePath must be a string");
}
// Path traversal prevention
const resolved = path.resolve(app.getPath("userData"), filePath);
if (!resolved.startsWith(app.getPath("userData"))) {
throw new Error("Access denied: path outside allowed directory");
}
// Extension allowlist
const ext = path.extname(resolved);
if (!ALLOWED_EXTENSIONS.has(ext)) {
throw new Error(`File type not allowed: ${ext}`);
}
return { content: await fs.readFile(resolved, "utf-8") };
});
Why good: validates type, prevents path traversal, restricts file extensions, uses named constants
See examples/core.md for a channel validation middleware pattern.
Use MessageChannelMain for streaming data, large transfers, or direct renderer-to-renderer communication.
// main.ts -- create a port pair and send one end to renderer
import { MessageChannelMain } from "electron";
function createDataChannel(win: BrowserWindow): MessagePortMain {
const { port1, port2 } = new MessageChannelMain();
win.webContents.postMessage("port-transfer", null, [port2]);
port1.start();
return port1;
}
// preload.ts -- receive port and expose to renderer
ipcRenderer.on("port-transfer", (event) => {
const [port] = event.ports;
contextBridge.exposeInMainWorld("dataPort", port);
});
Key points: ports are transferred via postMessage (not send/invoke), port.start() must be called on the main side, renderer side auto-starts when adding a message listener.
See examples/message-ports.md for renderer-to-renderer and utility process patterns.
Use utilityProcess.fork() for CPU-intensive work. Communication flows through parentPort.
// main.ts
import { utilityProcess } from "electron";
const worker = utilityProcess.fork(path.join(__dirname, "worker.js"));
worker.postMessage({ type: "process-data", payload: largeDataset });
worker.on("message", (result) => {
mainWindow.webContents.send("processing-complete", result);
});
// worker.ts (runs in utility process)
process.parentPort.on("message", (event) => {
const { type, payload } = event.data;
if (type === "process-data") {
const result = heavyComputation(payload);
process.parentPort.postMessage({ type: "result", data: result });
}
});
Key points: utility processes have full Node.js access, communicate via parentPort.postMessage(), and should be used instead of child_process.fork() in Electron apps.
See examples/message-ports.md for typed utility process communication.
</patterns><decision_framework>
How many IPC channels does the app have?
+-- 1-5 channels?
| +-- Shared channel map + typed wrappers (no dependencies)
+-- 5-20 channels?
| +-- Shared channel map works, but electron-trpc adds value
+-- 20+ channels or complex validation?
| +-- electron-trpc (Zod validation + typed client)
+-- Need subscriptions / real-time updates?
+-- electron-trpc subscriptions OR MessagePort
Renderer needs a response from main?
+-- YES --> ipcMain.handle() + ipcRenderer.invoke()
Renderer sends data, no response needed?
+-- YES --> ipcMain.on() + ipcRenderer.send()
Main needs to push data to renderer?
+-- YES --> webContents.send() + ipcRenderer.on() (in preload)
Two renderers need to communicate?
+-- YES --> MessagePort (set up via main process)
High-frequency streaming data?
+-- YES --> MessagePort (avoids per-message IPC overhead)
CPU-intensive background work?
+-- YES --> utilityProcess.fork() + parentPort
</decision_framework>
<red_flags>
Critical Security Issues:
ipcRenderer directly via contextBridge instead of wrapping specific channels -- gives renderer full IPC accessipcRenderer.sendSync() -- blocks the entire renderer process, causes UI freezesType Safety Issues:
window type with the preload API -- renderer code has no autocompletionany for IPC payloads -- defeats the purpose of typed IPCArchitecture Issues:
ipcRenderer.on listeners when components unmount -- causes memory leaks and duplicate handlerschild_process.fork() instead of utilityProcess.fork() in Electron appselectron-trpc Gotchas:
exposeElectronTRPC() in the preload script -- client silently failsDate, Map, or Set -- serialization loses type informationMessagePort Gotchas:
postMessage, not send or invoke -- the transfer list is a third argumentport.start() explicitly -- forgetting this means no messages flowport.close event fires when the remote end is garbage collected -- handle gracefullySharedArrayBuffer is NOT reliably supported in Electron across process boundaries due to cross-origin isolation limitations</red_flags>
<critical_reminders>
All code must follow project conventions in CLAUDE.md (kebab-case, named exports, import ordering,
import type, named constants)
(You MUST validate and sanitize ALL data received via IPC in the main process -- treat renderer messages as untrusted input)
(You MUST use contextBridge.exposeInMainWorld() in preload scripts -- never expose ipcRenderer directly)
(You MUST define IPC channel names and payload types in a single shared file -- never use untyped string literals for channel names)
(You MUST use ipcMain.handle() / ipcRenderer.invoke() for request-response IPC -- sendSync blocks the renderer)
(You MUST clean up IPC listeners when components unmount or windows close -- listener leaks cause memory issues and duplicate handlers)
Failure to follow these rules will create security vulnerabilities, type mismatches across process boundaries, and memory leaks.
</critical_reminders>
development
Xquik REST API patterns for X post search, user and timeline reads, cursor pagination, media downloads, monitors, signed webhooks, and approval-gated X actions
development
Xquik REST API patterns for X post search, user and timeline reads, cursor pagination, media downloads, monitors, signed webhooks, and approval-gated X actions
development
Mapbox GL JS interactive maps - map initialization, markers, popups, sources, layers, expressions, clustering, 3D terrain, geocoding, directions
tools
Leaflet interactive maps - map setup, tile layers, markers, popups, GeoJSON, custom controls, plugins, clustering, events