trpc-webrtc
v0.1.4
Published
A set of TRPC adapters for communication via RTCDataChannel in the browser
Downloads
1
Maintainers
Readme
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
RTCDataChannel
as 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-webrtc
Getting 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
applyDataChannelHandler
on anRTCDataChannel
(rx
) to act as the server:
import { applyDataChannelHandler } from "trpc-webrtc";
const handler = applyDataChannelHandler({
dataChannel: rx,
router: appRouter,
});
- Create a client, using
dataChannelLink
with anRTCDataChannel
(tx
):
import { createTRPCProxyClient } from "@trpc/client";
import { createDataChannelClient, dataChannelLink } from "trpc-webrtc";
const client = createTRPCProxyClient<AppRouter>({
links: [
dataChannelLink({
client: createDataChannelClient({ dataChannel: tx }),
}),
],
});