@hadeeb/trpc-worker

0.0.2 • Public • Published

tRPC in a Worker

Run tRPC in a web worker / electron main thread

Web Worker

Worker thread

import { applyWSSHandler } from "@trpc/server/adapters/ws";
import { createWorkerServer } from "@hadeeb/trpc-worker/adapter";
import { appRouter } from "../path/to/router";
import { createContext } from "../path/to/context";

applyWSSHandler({
  router: appRouter,
  createContext,
  wss: createWorkerServer({ worker: self }),
});

Main thread

import { createTRPCProxyClient, wsLink } from "@trpc/client";
import { createWorkerClient } from "@hadeeb/trpc-worker/link";
import type { AppRouter } from "../path/to/server/trpc";

const worker = new Worker("../path/to/trpc/worker");
const client = createTRPCProxyClient<AppRouter>({
  links: [
    wsLink({
      client: createWorkerClient({ worker }),
    }),
  ],
});

Electron

Main Process

import { ipcMain } from "electron";
import { createElectronServer } from "@hadeeb/trpc-worker/adapter";

applyWSSHandler({
  router: appRouter,
  createContext,
  wss: createElectronServer({ ipcMain }),
});

Preload script

import { ipcRenderer } from "electron";
import { trpcElectronPreload } from "@hadeeb/trpc-worker/adapter";

trpcElectronPreload({ ipcRenderer });

Browser script

const client = createTRPCProxyClient<AppRouter>({
  links: [
    wsLink({
      client: createWorkerClient({ worker: window }),
    }),
  ],
});

⚠️ Compatibility with tRPC Updates

This depends on the internals default tRPC Websocket link & ws adapter.

It helps keep the implementation very light.

However, it may break with tRPC patch updates.

Please check before updating tRPC.

Package Sidebar

Install

npm i @hadeeb/trpc-worker

Weekly Downloads

0

Version

0.0.2

License

MIT

Unpacked Size

22.3 kB

Total Files

23

Last publish

Collaborators

  • hadeeb