Inter-Process Communication (IPC)#
Paths beginning with src/ in this guide are relative to packages/desktop/.
The renderer runs sandboxed (contextIsolation: true, sandbox: true,
nodeIntegration: false — see src/main/config.ts), so it can't import
electron directly. All renderer↔main traffic goes through the typed
preload bridge exposed in src/preload/index.ts as window.electron.*
and a few sibling globals (window.fileUtils, window.path,
window.uploader, etc.).
Renderer calls use the contract in src/shared/types/ipc.ts for channel names, argument tuples and result types. Main-process implementations must separately satisfy those declared result shapes; Electron's handler registrations do not enforce the maps automatically. See TYPESCRIPT.md for the
TypeScript-side details.
Channel naming
Renderer↔main channels are prefixed with mt:: (e.g. mt::fs::stat,
mt::open-new-tab). Internal main-process events (for example, language-changed) are separate from
renderer IPC and do not all use this prefix. Declare new renderer↔main channels
with mt:: in the shared contract.
The four channel categories
src/shared/types/ipc.ts defines four interfaces:
| Interface | Direction | Semantics |
|---|---|---|
IpcInvokeChannels | renderer → main | Promise<T> round-trip (ipcMain.handle) |
IpcSendChannels | renderer → main | fire-and-forget (ipcMain.on) |
IpcSyncChannels | renderer → main | synchronous reply (event.returnValue) |
IpcMainEventChannels | main → renderer | push event (webContents.send / on) |
Each entry tells you the args tuple and (for invoke/sync) the return type:
'mt::fs::stat': { args: [path: string]; ret: SerializedStat }
'mt::format-link-click': [payload: { data: unknown; dirname: string }] // send shapeRenderer side
Use the global window.electron.ipcRenderer (the typed wrapper exposed
by the preload bridge). Don't import { ipcRenderer } from 'electron' —
it isn't available under sandboxing.
// Round-trip
const stat = await window.electron.ipcRenderer.invoke('mt::fs::stat', fullPath)
// Fire-and-forget
window.electron.ipcRenderer.send('mt::format-link-click', { data, dirname })
// Subscribe to a main → renderer push event (returns an unsubscribe fn)
const off = window.electron.ipcRenderer.on('mt::screenshot-captured', () => {
// …
})
off()once(channel, listener) also returns an unsubscribe function. removeAllListeners(channel) removes subscriptions for that channel and returns nothing. For
filesystem and path helpers, prefer the typed convenience APIs already
exposed via window.fileUtils.*, window.path.*, window.uploader.*,
etc. — they wrap the underlying mt::fs::* / mt::uploader::* channels
and keep call sites short.
Main side
Channels are wired with ipcMain.handle (invoke), ipcMain.on (send /
sync), or webContents.send (push):
import { ipcMain } from 'electron'
ipcMain.on('mt::format-link-click', (_event, { data, dirname }) => {
// …
})
// Push to a specific renderer window:
window.webContents.send('mt::open-new-tab', tabPayload, options, selected)Channels that exist because the renderer is sandboxed
A few capabilities are main-only, and the renderer reaches them through a narrow channel rather than a general one:
| Channel | Purpose | Note |
|---|---|---|
mt::dialog::show-save | Ask the user where to write a file | Only title, defaultPath and filters cross the bridge; the write itself goes through mt::fs::write-file. |
mt::clipboard::write-image | Put a PNG on the system clipboard | Uses Electron's main-process clipboard API; the bridge also exposes separate text helpers. |
mt::diagram::fetch-plantuml | Retrieve a PlantUML render | The renderer CSP restricts connections to its own origin. The main-process handler fetches external PlantUML image data for export. It sends the server and the encoded diagram separately and main composes the URL, from the plantumlServer preference rather than from what was sent — a free-form URL is never accepted, redirects are refused, and the response is capped as it arrives. |
Adding a new channel
- Add an entry to the appropriate interface in
src/shared/types/ipc.ts(pick invoke / send / sync / main-event based on the direction and shape). - Wire the main-process handler in
src/main/ipc/*.ts(or the relevant feature module). - Call it from the renderer through
window.electron.ipcRenderer.*or, if it deserves a dedicated facade, expose a method on one of the typed bridges insrc/preload/index.ts.
Run pnpm typecheck after changing the contract, and review the main handler's serialization and result type as well as renderer call sites.
