anan7A
tRPC3y ago
2 replies
anan7

Websocket is not defined error

I'm getting a "WebSocket is not defined error" on my next app connected to an express backend. Any idea what I'm doing wrong?

My server/index file is
import * as trpcExpress from "@trpc/server/adapters/express"; import { applyWSSHandler } from "@trpc/server/adapters/ws"; import express from "express"; import cors from "cors"; import { appRouter } from "./routers/_app"; import { createContext } from "./context"; import ws from 'ws' const app = express(); app.use(cors({ origin: ["http://localhost:3000", "http://localhost:3001"] })); app.use( "/trpc", trpcExpress.createExpressMiddleware({ router: appRouter, createContext, }) ); const server = app.listen(8000); applyWSSHandler({ wss: new ws.Server({ server }), router: appRouter, createContext })

My context file is
import { inferAsyncReturnType } from "@trpc/server"; import * as trpcExpress from "@trpc/server/adapters/express"; import { NodeHTTPCreateContextFnOptions } from '@trpc/server/adapters/node-http'; import { IncomingMessage } from 'http'; import ws from 'ws'; export const createContext = ({ req, res }: trpcExpress.CreateExpressContextOptions | NodeHTTPCreateContextFnOptions<IncomingMessage, ws>) => { return ({ req, res }) } export type Context = inferAsyncReturnType<typeof createContext>;
Was this page helpful?