Introduction
electron-messageport-trpc is a transport layer that connects tRPC v11 with Electron’s MessagePort API. It enables type-safe IPC between Electron processes using tRPC’s familiar patterns.
Why MessagePort + tRPC?
Section titled “Why MessagePort + tRPC?”Traditional Electron IPC using ipcMain / ipcRenderer has limitations:
- Star topology — all communication must pass through the main process
- No type safety — channel names are plain strings with no compile-time checks
- JSON serialization —
Date,Map,Set, and other complex types are lost
This library solves all three problems:
| Feature | ipcMain/ipcRenderer | electron-messageport-trpc |
|---|---|---|
| Topology | Star (main as hub) | Flexible (renderer-to-main, main-to-utility, renderer-to-utility) |
| Type safety | None | Full end-to-end via tRPC |
| Serialization | JSON.stringify | Structured Clone (native Date, Map, Set) |
| Subscriptions | Manual event listeners | tRPC subscriptions (async iterables) |
How It Works
Section titled “How It Works”The library uses Electron’s MessageChannelMain to establish direct MessagePort connections between processes. A lightweight message protocol carries tRPC requests and responses over these ports.
Renderer Main Process | | | MessagePort (port1) | |<========================>| | tRPC requests/results | | |- The main process creates a
MessageChannelMainand sends one port to the renderer viawebContents.postMessage. - The preload script receives the port and exposes it to the renderer via
contextBridge. - The renderer uses the port as a tRPC link, gaining full type-safe RPC with subscriptions.
Architecture Overview
Section titled “Architecture Overview”┌─────────────┐ MessagePort ┌──────────────┐│ Renderer │<===============>│ Main Process │└─────────────┘ └──────┬───────┘ │ MessagePort │ ┌───────┴────────┐ │ Utility Process │ └─────────────────┘In advanced topologies, the main process can also hand a port off to a utility process so the main thread stays out of the request path.
Next Steps
Section titled “Next Steps”- Installation — add the package to your project
- Quick Start — set up a working renderer-to-main example in minutes