Finn
Finn6mo ago

How do I setup subscriptions with websockets in Next.js 14 app router?

All I've found were some older examples for the pages router which for me were not very understandable or I'm just stupid. For the most part I'm having trouble understanding how exactly the websocket server is started. Does it start with Next.js or is it something that you have to seperatly? Would be glad if someone can give me some insight.
Solution:
btw i did it: You can check out the project. it uses app directory. I am using t3 stack here https://github.com/Roopaish/evento main files to look at are: tsconfig.server.json, src/server/ws-dev-server.ts (need to update prod one, will do later), trpc/react.tsx, src/server/routers/post.ts, app/(all)/post/page.tsx And also don't export thing from wss-dev-server.ts. Cause if you use those exports, next js server will try to use that port to create new WebSocketServer....
Jump to solution
3 Replies
roopaish
roopaish4mo ago
The web socket needs to be started separately. I am also confused on how they communicate. I am trying out different solutions to set this up in my app. I did implemented it and was working but authentication with next-auth (getServerSession) did not work, so i am trying different things from here and there to make it work.
Solution
roopaish
roopaish4mo ago
btw i did it: You can check out the project. it uses app directory. I am using t3 stack here https://github.com/Roopaish/evento main files to look at are: tsconfig.server.json, src/server/ws-dev-server.ts (need to update prod one, will do later), trpc/react.tsx, src/server/routers/post.ts, app/(all)/post/page.tsx And also don't export thing from wss-dev-server.ts. Cause if you use those exports, next js server will try to use that port to create new WebSocketServer.
Finn
Finn4mo ago
Cheers mate, thanks for sharing.
More Posts
Why is my tRPC + Next 14 (app router) data fetching pattern not refreshing the UI?### Goals of this post: - to know why my UI is not refreshing after i mutate the backend - tUsing tRPC in Next.js MiddlewareHello, I am quite new to tRPC so forgive me if I'm asking something quite obvious/dumb. I was introneed help refreshing websocketWe currently are using tRPC w/ react and websockets. We’re using the URL of the websocket as the autrouter is crashing when in separate fileWhen i use router merging and have e.g users router in separate file, i import router from trpc.ts aHow do I pass a Generic to a trpc query procedure?I want to to something like this: ```ts type AppIdsWithConfig = typeof kodixCareAppId | typeof calenIs there something to be done about trpc errors and solidjs/seroval?Basically if you `throw error` in trpc route, solidjs seroval fails to serialize it during SSR. I doHow can i createCaller from a NextJs App Router if my server uses express tRPC adapter?I'm using the express adapter for the server side of tRPC, and the client is a NextJS AppRouter app.Is there a way to refetch a query with new parameters?Hi I'm using tRPC in a Next.js app and I have a button that a user can click to get the latest dataIs there any benefit to putting the db connection in the context versus having it as an global var?Can I do this? ```ts export const challengesRouter = router({ getChallengeById: privateProcedure Controller is already closed crashIm using trpc with nextjs 14 app router and started to see that my app crashes due to such error. Di