Safe Electron IPC patterns for renderer-main communication with preload bridges, channel design, replies, and serialization limits.
81%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
Passed
No findings from the security scan
Use this skill when you need to design or implement communication between Electron's main and renderer processes.
ipcMain and ipcRenderer.contextBridge
instead of exposing raw Electron modules to the renderer.openFile() or
setTitle(title).ipcRenderer.send, ipcRenderer.invoke, or ipcRenderer.on
directly to renderer code.dialog:openFile for readability.| Need | Renderer API | Main API | Notes |
|---|---|---|---|
| Fire-and-forget renderer -> main | ipcRenderer.send | ipcMain.on | Good for commands with no result |
| Request/response renderer -> main | ipcRenderer.invoke | ipcMain.handle | Preferred two-way pattern |
| Push main -> renderer | preload listener wrapper | webContents.send | Main must target a specific window/webContents |
| Renderer -> renderer | relay through main or MessagePort | main as broker if needed | No direct IPC channel between renderers |
Use this when the renderer triggers a main-process action and does not need a result.
ipcMain.on('set-title', (event, title) => {
const win = BrowserWindow.fromWebContents(event.sender)
win?.setTitle(title)
})contextBridge.exposeInMainWorld('electronAPI', {
setTitle: (title) => ipcRenderer.send('set-title', title)
})window.electronAPI.setTitle('New title')Use this when the renderer needs a result from the main process. Prefer this over older reply patterns.
ipcMain.handle('dialog:openFile', async () => {
const { canceled, filePaths } = await dialog.showOpenDialog()
return canceled ? undefined : filePaths[0]
})contextBridge.exposeInMainWorld('electronAPI', {
openFile: () => ipcRenderer.invoke('dialog:openFile')
})const filePath = await window.electronAPI.openFile()invoke/handle is the preferred async request-response model.ipcMain.handle are serialized; the renderer does not receive the
full original error object.Use this when the main process needs to push updates into a specific window.
mainWindow.webContents.send('update-counter', 1)contextBridge.exposeInMainWorld('electronAPI', {
onUpdateCounter: (callback) => {
ipcRenderer.on('update-counter', (_event, value) => callback(value))
}
})window.electronAPI.onUpdateCounter((value) => {
// update UI
})There is no invoke equivalent from main to renderer. If the renderer needs to
reply, send a new message back to the main process on a separate channel.
Avoid these unless you have a strong compatibility reason:
ipcRenderer.send plus event.reply for two-way communication:
works, but forces manual reply-channel bookkeeping.ipcRenderer.sendSync: blocks the renderer and hurts responsiveness.ipcRenderer methods over contextBridge.Electron IPC uses the Structured Clone Algorithm.
BrowserWindow or
WebContents, and Node/Electron objects backed by native C++ classes.Pass serializable data, not live framework objects.
send + oninvoke + handlewebContents.sendMessagePorttympanix/Electorrent
20ec40c
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.