Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

azamuddin/electron-trpc

Repository files navigation

electron-trpc

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 electron-trpc

# Using yarn
yarn add electron-trpc

# Using npm
npm install --save electron-trpc

Basic Setup

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

    { 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] }); });">import { app } from 'electron';
    import { createIPCHandler } from 'electron-trpc/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:

    { exposeElectronTRPC(); });">import { exposeElectronTRPC } from 'electron-trpc/main';

    process.once('loaded', async () => {
    exposeElectronTRPC();
    });

    Note: electron-trpc 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 { createTRPCProxyClient } from '@trpc/client';
    import { ipcLink } from 'electron-trpc/renderer';

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

About

Build type-safe Electron inter-process communication using tRPC

Resources

Readme

License

MIT license

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

Languages

  • TypeScript 98.6%
  • Other 1.4%