nextjs 14 app router TRPC websocket subscriptions server setup

71 views Asked by At

I am using nextJS 14 app router in combination with TRPC. I've been trying to implement TRPC's websocket subscriptions feature, however I've found little up to date documentation on how to do so.

One resource I did stumble across was [this repo] (https://github.com/trpc/examples-next-prisma-websockets-starter) maintained by trpc's developers, however it does not use the app router. In it, they have two seperate files of note prodServer.ts & wssDevServer.ts in src/server/. Ideally there'd only be one file that serves to setup the ws server for both production and development, why is it that they use a separate file for each here?

If I implement the development server in my own enviroment, my sockets instantly close after connection as an error occurs during the createContext function called within the applyWSSHandler. It seems to be occuring when any server-side only functions are called (such as cookies(), headers() or redirect()), causing my server to 500 error and for the socket connection to be closed.

I'd like to be sure I'm implementing TRPC's subscriptions to my nextJS app as effectively as possible, any knowledge is appreciated.

0

There are 0 answers