Skip to content

Preload API

import { exposePortReceiver } from 'electron-messageport-trpc/preload';

Sets up the preload script to receive a MessagePort from the main process and expose it to the renderer.

  1. Listens on the internal IPC channel for a port sent by createRendererPort().
  2. Uses contextBridge.exposeInMainWorld() to expose a window.electronTRPCPort object with a requestPort() method.
  3. When the renderer requests a port, forwards the transferred MessagePort into the renderer’s main world via window.postMessage().

None.

void

preload.ts
import { exposePortReceiver } from 'electron-messageport-trpc/preload';
exposePortReceiver();

After calling exposePortReceiver(), the renderer has access to:

window.electronTRPCPort.requestPort(): void

You do not need to use this directly — the getPort() function from electron-messageport-trpc/renderer wraps it for you.

The function uses contextBridge.exposeInMainWorld() only for the request trigger. The actual MessagePort is transferred with window.postMessage(), so the renderer still receives a real browser MessagePort.