Run tRPC in a web worker / electron main 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 }),
});
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 }),
}),
],
});
import { ipcMain } from "electron";
import { createElectronServer } from "@hadeeb/trpc-worker/adapter";
applyWSSHandler({
router: appRouter,
createContext,
wss: createElectronServer({ ipcMain }),
});
import { ipcRenderer } from "electron";
import { trpcElectronPreload } from "@hadeeb/trpc-worker/adapter";
trpcElectronPreload({ ipcRenderer });
const client = createTRPCProxyClient<AppRouter>({
links: [
wsLink({
client: createWorkerClient({ worker: window }),
}),
],
});
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.