Skip to content

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.

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 serializationDate, Map, Set, and other complex types are lost

This library solves all three problems:

FeatureipcMain/ipcRendererelectron-messageport-trpc
TopologyStar (main as hub)Flexible (renderer-to-main, main-to-utility, renderer-to-utility)
Type safetyNoneFull end-to-end via tRPC
SerializationJSON.stringifyStructured Clone (native Date, Map, Set)
SubscriptionsManual event listenerstRPC subscriptions (async iterables)

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 |
| |
  1. The main process creates a MessageChannelMain and sends one port to the renderer via webContents.postMessage.
  2. The preload script receives the port and exposes it to the renderer via contextBridge.
  3. The renderer uses the port as a tRPC link, gaining full type-safe RPC with subscriptions.
┌─────────────┐ 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.

  • Installation — add the package to your project
  • Quick Start — set up a working renderer-to-main example in minutes