tRPCttRPC
Powered by
kalK
tRPC•2y ago•
9 replies
kal

Context with trpc ws client

Hello! I've implemented trpc to my nextjs 14 app router project, along with trpc socket subscriptions. I recently started making use of trpc context and implemented an asyncrhinous check to my session handler in order to append session information to my trpc context. This all works fine for the majority of my trpc functionality (non websocket stuff), however my clients are unable to establish a websocket connection to the server as every time the initial upgrade request is sent, the server 500 errors due to the added context initialisation.

The error seems to be caused because some server component functionality ( cookies() ) is called during the call to fetch session information during context initialisation for the socket. Why does this cause an error when initliasing the trpc websocket client and not when making a standard trpc request?

I should note that I followed this example to initialise my seperate websocket server. ( ws server is hosted on port 3001, main next app is on port 3000, could this possible be a cors issue?) https://github.com/trpc/examples-next-prisma-websockets-starter/blob/main/src/server/wssDevServer.ts

I've attached most of my core trpc initialisation code.

any help would be greatly appreciated, I've been toying with this issue for days now and it is really hindering my project's development. Thanks.
image.png
image.png
image.png
image.png
image.png
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

WS with TRPC
acronieAacronie / ❓-help
3y ago
Multiple Clients/Services In tRPC Context
isaac_wayIisaac_way / ❓-help
4y ago
Can we bundle WS TRPC Server?
AndrésAAndrés / ❓-help
13mo ago
tRPC context, NeonDB & WebSockets
kgniKkgni / ❓-help
2y ago