Preload API
import { exposePortReceiver } from 'electron-messageport-trpc/preload';exposePortReceiver()
Section titled “exposePortReceiver()”Sets up the preload script to receive a MessagePort from the main process and expose it to the renderer.
What It Does
Section titled “What It Does”- Listens on the internal IPC channel for a port sent by
createRendererPort(). - Uses
contextBridge.exposeInMainWorld()to expose awindow.electronTRPCPortobject with arequestPort()method. - When the renderer requests a port, forwards the transferred MessagePort into the renderer’s main world via
window.postMessage().
Parameters
Section titled “Parameters”None.
Returns
Section titled “Returns”void
Example
Section titled “Example”import { exposePortReceiver } from 'electron-messageport-trpc/preload';
exposePortReceiver();What Gets Exposed
Section titled “What Gets Exposed”After calling exposePortReceiver(), the renderer has access to:
window.electronTRPCPort.requestPort(): voidYou do not need to use this directly — the getPort() function from electron-messageport-trpc/renderer wraps it for you.
Security
Section titled “Security”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.