mwinelM
tRPC2y ago
15 replies
mwinel

Running Websockets/Subscriptions with Fetch / Edge Runtimes Adapter

Hi, has anyone been able to use trpc websockets/subscriptions? I am having challenges setting up the ws server using Fetch / Edge Runtimes Adapter and running it either in dev or production mode. Tried to follow the docs but it does not highlight this bit.

I am using Node 20> and pnpm.

Here is my route handler

import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import { createContext } from "./context";
import { apiRouter } from "./router";

export const trpcApiRouteHandler = (req: Request) =>
  fetchRequestHandler({
    endpoint: "/api",
    req,
    router: apiRouter,
    createContext,
  });


Here is my sample router module

import type {} from "@prisma/client";
import * as authProcedures from "../modules/auth/procedures";
import * as aiProcedures from "../modules/ai/procedures";
import { router } from "./base";

export const apiRouter = router({
  auth: router(authProcedures),
  ai: router(aiProcedures),
});


And finally here is my base module

import { TRPCError, initTRPC } from "@trpc/server";
import superjson from "superjson";

const t = initTRPC.context<Context>().create({
  transformer: superjson,
});

export const router = t.router;
Was this page helpful?