Skip to content

mat-sz/trpc-electron

 
 

Repository files navigation

trpc-electron

Note

This is a fork of jsonnull/electron-trpc for TRPC v11.x.x since that version introduces many breaking changes.

NPM MIT

Build IPC for Electron with tRPC

  • Expose APIs from Electron's main process to one or more render processes.
  • Build fully type-safe IPC.
  • Secure alternative to opening servers on localhost.
  • Full support for queries, mutations, and subscriptions.

Installation

# Using pnpm
pnpm add trpc-electron

# Using yarn
yarn add trpc-electron

# Using npm
npm install --save trpc-electron

Basic Setup

  1. Add your tRPC router to the Electron main process using createIPCHandler:

    import { app } from 'electron';
    import { createIPCHandler } from 'trpc-electron/main';
    import { router } from './api';
    
    app.on('ready', () => {
      const win = new BrowserWindow({
        webPreferences: {
          // Replace this path with the path to your preload file (see next step)
          preload: 'path/to/preload.js',
        },
      });
    
      createIPCHandler({ router, windows: [win] });
    });
  2. Expose the IPC to the render process from the preload file:

    import { exposeElectronTRPC } from 'trpc-electron/main';
    
    process.once('loaded', async () => {
      exposeElectronTRPC();
    });

    Note: trpc-electron depends on contextIsolation being enabled, which is the default.

  3. When creating the client in the render process, use the ipcLink (instead of the HTTP or batch HTTP links):

    import { createTRPCClient } from '@trpc/client';
    import { ipcLink } from 'trpc-electron/renderer';
    
    export const client = createTRPCClient({
      links: [ipcLink()],
    });
  4. Now you can use the client in your render process as you normally would (e.g. using @trpc/react).

About

Fork of electron-trpc for trpc 11.x.x

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 100.0%