Why processes need to communicate
IPC means interprocess communication. An application can consist of multiple processes. In Electron, the main process manages windows and application lifecycle, while renderer processes run pages. They normally have separate address spaces. A React component cannot directly access variables or functions in the main process as if it were importing another JavaScript module. Selecting a local file, changing a service port, or receiving generated model output requires information to cross process boundaries.1
Before developing PromptPulse, I already knew that communication inside an application could have lower overhead than typical frontend/backend network interaction. I wanted to see how to use those approaches in Electron. I found relevant material in Electron’s official docs and a node-llama-cpp example and kept investigating. On March 2, 2025, I asked Cursor in the electron_play workspace:
is ipc message faster than requests? can i use it to replace requests in electron
— From my conversation with Cursor, with original spelling retained.2
In that question IPC means Electron’s communication interface. More broadly, sockets are themselves an IPC mechanism. What I was comparing was Electron messaging with calls such as local HTTP. It doesn’t reduce to “is IPC faster than sockets?”
What do pipes, sockets, and shared memory do?
Different IPC mechanisms suit different data and leave different responsibilities to the programmer. The examples below describe common operating-system mechanisms without assuming Electron uses any particular one internally.
| Mechanism | How information moves | What to watch for |
|---|---|---|
| Pipes and named pipes | One end writes and the other reads, as when a parent process reads a child’s stdout | A POSIX pipe, for example, is a one-way byte stream without message boundaries. You need a convention for where a message ends |
| Sockets | Communication endpoints send and receive data over a network or solely on the local machine | TCP and Unix domain sockets differ. The latter connect local processes without involving a remote server |
| Shared memory | The same memory is mapped into multiple processes’ address spaces for reading and writing | Agree on a layout and coordinate access with locks, semaphores, or similar mechanisms to avoid seeing half-written data |
The operating-system manuals describe the basic semantics of pipes, Unix domain sockets, and POSIX shared memory.345 HTTP and WebSocket operate at a higher layer, organizing requests, responses, or messages. Choosing Electron IPC means using an interface the framework wraps. It doesn’t bypass the operating system.
Shared memory can reduce some copying but requires synchronization. Pipes and sockets give you channels to read and write, while leaving buffering, message boundaries, and peer exit to consider. For a chat client, the choice also depends on call frequency, payloads, and whether independent programs need to reuse the interface.
How does this communication relate to system calls?
Application code usually runs in user mode. The operating-system kernel runs with greater privilege in kernel mode and manages memory, schedules processes, and accesses devices. To create a pipe, establish a socket, or map shared memory, an application requests those facilities through OS interfaces. On Linux, system calls are the fundamental application-to-kernel interface and are usually wrapped by a runtime library.6
Think of a system call as a controlled request for kernel work. It doesn’t call a business function in another application. Entering the kernel doesn’t necessarily switch to another process: switching between user and kernel mode and a scheduler’s process switch are different concepts.
Shared memory still involves the kernel. Creating a mapping requires arranging address spaces and permissions. Once mapped, ordinary reads and writes on accessible pages don’t require a system call per byte. Page faults, waiting on synchronization, and similar events can still involve the kernel.57
In Electron, developers call JavaScript APIs such as ipcRenderer.invoke(). The framework handles sending messages and returning results, while the runtime and OS do the underlying work. A JavaScript invocation, message exchange, and system call do not map one to one. The API name alone doesn’t establish a copy count or performance.
I had a reason to investigate communication overhead, but no saved benchmarks establishing that Electron IPC is always faster than every socket-based alternative. Its more immediate benefit was avoiding a separate HTTP interface just for local capabilities called by the UI.
How do React, preload, and the main process connect?
I also asked whether React could communicate through window.2 React doesn’t change Electron’s process boundaries. The page runs in a renderer process and calls local functionality through an interface exposed by preload.
There are two boundaries. contextBridge connects the preload and page JavaScript contexts. IPC connects the renderer and main processes. Preload is not another process. With contextIsolation, preload and the page also see different window objects, so assigning window.someAPI = ... in preload is not a reliable way to expose an interface.8
The official tutorial’s call path starts with a page method on window.electronAPI. Preload supplies that method using contextBridge.exposeInMainWorld(). The method sends an IPC message, and the main process runs the corresponding handler.1
I kept revisiting three steps to complete this path: register the main-process handler, expose the interface through preload, and invoke it from the page. An ordinary button and a React component both use the same page interface.
To inspect the interface, open that window’s developer tools and enter typeof window.electronAPI?.setTitle in Console. With the March 4 PromptPulse code, "function" means the page can see the method. To confirm the message reached the main process, check whether the matching ipcMain.handle handler was invoked.9
Electron’s REPL tutorial explains where JavaScript runs interactively. A window’s Console evaluates expressions in the renderer process. On Linux or macOS, running ./node_modules/.bin/electron --interactive in a terminal starts a new Electron main process where you can try main-process APIs.10
How do send, invoke, and notifications from the main process differ?
| Need | Sender and receiver | What the return value means |
|---|---|---|
| Page sends a notification | ipcRenderer.send → ipcMain.on | No automatic business result. Design a reply message if one is needed |
| Page requests a result | ipcRenderer.invoke → ipcMain.handle | Returns a promise for the handler’s return value or asynchronous result |
| Main process notifies page | webContents.send → ipcRenderer.on | The page receives through a listener. Suitable for status and progress updates |
invoke handles request/reply matching. It isn’t a continuous push stream. When a model generates text incrementally, send additional update messages. The official docs also note that synchronous sendSync blocks the renderer until a response and is a poor default for UI calls.1
A returned promise doesn’t automatically establish that the business operation completed. In PromptPulse’s March 4 code, preload exposed window.electronAPI.setTitle but actually used the set-title channel to request a new port for an Express service. The main process called restartOnNewPort and immediately returned the fixed string "default" without waiting for the restart. The IPC response only showed the handler had returned. It didn’t confirm the new port was listening.9
The code also shows both communication paths surviving: a local HTTP service handles API calls, while IPC can control that service. Investigating IPC didn’t replace all HTTP requests in the project. For the experience of service lifecycle and model delivery, see PromptPulse development notes.
How does birpc turn messages into bidirectional function calls?
Reading node-llama-cpp’s Electron example, I found another wrapper: birpc. RPC means remote procedure call. “Remote” can mean another process on the same machine. It needn’t mean another server. The caller sends a request and waits for its result in a form resembling a function call. A system call requests a kernel service. RPC organizes application calls between communicating endpoints. They operate at different levels.
Several files show the division of responsibilities:11
| File | Role |
|---|---|
createElectronSideBirpc.ts | Uses createBirpc to wrap Electron’s send and listen interfaces and checks that messages come from the expected window |
preload.ts | Exposes the renderer-side IPC interface through contextBridge |
llmRpc.ts | Provides methods for selecting GGUF, loading a model, sending prompts, stopping generation, and resetting history, and notifies the UI of model state changes |
post connects to window.webContents.send, on to ipcMain.on, and serialization and deserialization use JSON.stringify and JSON.parse. birpc organizes function calls on top of messages. It doesn’t replace the underlying transport.
I also read birpc’s test/index.test.ts. It’s smaller than a complete client and makes it easier to see both sides register functions, call each other, and wait for results. In a version available before March 2025, the test used Node’s MessageChannel to connect Alice and Bob. bob.hello() called Alice’s exposed function, while alice.hi() called Bob’s. eventNames: ['bump'] marked bump as an event that doesn’t wait for a response.12
That test connects two message ports inside one test program. It does not start two independent processes. It demonstrates birpc’s call convention. Wiring the same post and on to Electron IPC in the example creates the cross-process call of interest here. In a model client this can organize both “the UI requests a model operation” and “the model side notifies the UI of an update.”
I studied the example, but my own implementation centered on local Express endpoints and sendPrompt. The records don’t establish that I brought the birpc architecture into PromptPulse.911
Communication security: permission to send a message isn’t permission to do everything
Operating-system process isolation can’t decide which business calls an app should allow. If a page can ask the main process to execute arbitrary commands over IPC, the main process can become a way around the privilege boundary. Even a local page might render external content or suffer script injection.
Electron context isolation and IPC interface design need to work together:813
- Expose specific business methods only. Offer
selectModel()orchangePort(port). Don’t hand the page arbitrary-channelsendandinvoke.contextBridgedoesn’t automatically enforce application permissions. - Validate sender and arguments in the main process. Check the window, frame, or page origin, then validate port ranges, file paths, and authorization for the operation. TypeScript hints aren’t runtime checks.
- Pass callbacks only the data they need. When preload wraps a listener, keep Electron’s raw event object out of the page to avoid exposing extra capabilities.
When reading source, separate “shows how the calls work” from “safe to copy as a permission boundary”. The node-llama-cpp v3.6.0 example checked the sender’s window, but preload exposed fairly general on, off, send, and invoke wrappers and passed along the event object. It illustrates how birpc connects to Electron. For a real client, narrow the exposed capabilities according to the official docs and check frame origin and business arguments.1113
Subscriptions also need lifecycle management. Remove listeners when React components unmount to avoid duplicate delivery. That’s separate from authorization. What counts as a completed operation and what happens to requests when a window closes also need application-level conventions. An RPC wrapper simplifies call syntax without automatically resolving these business and permission questions.
References
Footnotes
-
Electron contributors. (n.d.). Inter-Process Communication [Pattern 1–3 及 preload 示例]. Electron documentation. https://www.electronjs.org/docs/latest/tutorial/ipc 。技术说明核对 v34.3.0 固定版本。作者提供的访问顺序:主进程到渲染进程 → 渲染进程到主进程,单向 → IPC 总览 → 再次查看主进程到渲染进程 → ipcMain.on → 通过 preload 暴露 ipcRenderer.send → 构建渲染进程 UI → 通过 preload 暴露 ipcRenderer.invoke → ipcMain.handle。原记录中
ww.electronjs.org按同一文档修正为www.electronjs.org。Access date: June 8, 2025 (historical cutoff). ↩ ↩2 ↩3 -
CaiBorui. (2025, March 2). Creating and Integrating a React App [与 Cursor 的未公开开发对话,作者留存]. 会话
3c26bdb1-781a-4d59-987d-c558a926be71属于/home/borui/my_dev/electron_play,不是其下的 PromptPulse 仓库目录。正文引文来自 IPC 是否能替代请求的提问,相邻助手回复为北京时间 03:02:56;React 使用 window 通信的提问相邻回复为 03:04:22。用户消息没有独立时间戳。 ↩ ↩2 -
Linux man-pages contributors. (n.d.). pipe(7) [“DESCRIPTION”及“I/O on pipes and FIFOs”]. man7.org. https://man7.org/linux/man-pages/man7/pipe.7.html 。补充基础概念参考。Access date: June 8, 2025. ↩
-
Linux man-pages contributors. (n.d.). unix(7) [“DESCRIPTION”]. man7.org. https://man7.org/linux/man-pages/man7/unix.7.html 。补充基础概念参考。Access date: June 8, 2025. ↩
-
Linux man-pages contributors. (n.d.). shm_overview(7) [“DESCRIPTION”及“NOTES”]. man7.org. https://man7.org/linux/man-pages/man7/shm_overview.7.html 。补充基础概念参考。Access date: June 8, 2025. ↩ ↩2
-
Linux man-pages contributors. (n.d.). syscalls(2) [“DESCRIPTION”及“System calls and library wrapper functions”]. man7.org. https://man7.org/linux/man-pages/man2/syscalls.2.html 。补充基础概念参考。Access date: June 8, 2025. ↩
-
Linux man-pages contributors. (n.d.). mmap(2) [“DESCRIPTION”,MAP_SHARED 与映射权限]. man7.org. https://man7.org/linux/man-pages/man2/mmap.2.html 。补充基础概念参考。Access date: June 8, 2025. ↩
-
Electron contributors. (n.d.). Context Isolation [“What is it?”及“Security considerations”,v34.3.0]. GitHub. https://github.com/electron/electron/blob/v34.3.0/docs/tutorial/context-isolation.md 。补充技术参考。Access date: June 8, 2025. ↩ ↩2
-
CaiBorui [S2thend]. (2025, March 4). PromptPulse [提交 32c04f1,preload 与主进程 IPC 处理]. GitHub. desktop/preload.js;desktop/main.js。Access date: June 8, 2025. ↩ ↩2 ↩3
-
Electron contributors. (n.d.). REPL [“Main process” 与 “Renderer process”]. Electron documentation. 主进程和渲染进程的交互式入口。Access date: February 27, 2025. ↩
-
node-llama-cpp contributors. (n.d.). Electron TypeScript React template [源码:createElectronSideBirpc.ts、preload.ts、llmRpc.ts]. GitHub. 作者提供的浏览入口:createElectronSideBirpc.ts、preload.ts、llmRpc.ts。Access date: June 8, 2025 (historical cutoff). 按 v3.6.0 对应文件核对:IPC 与 birpc 封装、preload 桥接、模型操作与状态通知。描述示例的职责,不将当前 master 当作历史快照,也不据此认定项目采用了相同 RPC 架构。 ↩ ↩2 ↩3
-
Fu, A., & birpc contributors. (2024, October 8). index.test.ts [历史快照 a8aced6,“createChannel”“basic”测试]. GitHub. 作者提供的浏览入口:https://github.com/antfu-collective/birpc/blob/HEAD/test/index.test.ts ;固定快照。日期为该历史文件的提交日期,不是作者浏览日期;HEAD 会变化,不作为当时页面快照。Access date: June 8, 2025. ↩
-
Electron contributors. (n.d.). Security [“Validate the sender of all IPC messages”,v34.3.0]. GitHub. https://github.com/electron/electron/blob/v34.3.0/docs/tutorial/security.md#17-validate-the-sender-of-all-ipc-messages 。补充技术参考。Access date: June 8, 2025. ↩ ↩2