0.1.4 • Published 3 years ago
trpc-webrtc v0.1.4
trpc-webrtc
A set of tRPC adapters to enable type-safe communication via RTCDataChannel in the browser.
- Compatible with tRPC
>=10.20.0. - Use any
RTCDataChannelas an in-browser tRPC server. - Full support for queries, mutations, and subscriptions.
Installation
# Using pnpm
pnpm add trpc-webrtc
# Using yarn
yarn add trpc-webrtc
# Using npm
npm install --save trpc-webrtcGetting Started
- Initialize tRPC, with
allowOutsideOfServer: true:
import { initTRPC } from "@trpc/server";
const t = initTRPC.create({ allowOutsideOfServer: true });- Create a router, as usual:
const appRouter = t.router({
testQuery: t.procedure.query(() => ({ hello: "world" })),
});
type AppRouter = typeof appRouter;- Invoke
applyDataChannelHandleron anRTCDataChannel(rx) to act as the server:
import { applyDataChannelHandler } from "trpc-webrtc";
const handler = applyDataChannelHandler({
dataChannel: rx,
router: appRouter,
});- Create a client, using
dataChannelLinkwith anRTCDataChannel(tx):
import { createTRPCProxyClient } from "@trpc/client";
import { createDataChannelClient, dataChannelLink } from "trpc-webrtc";
const client = createTRPCProxyClient<AppRouter>({
links: [
dataChannelLink({
client: createDataChannelClient({ dataChannel: tx }),
}),
],
});