tRPC splitLink wsLink/httpBatchLink explanation please

I am still struggling to fully understand which approach should I use for tRPC subscriptions. Currently I have a an app created with create-3t-app with websocket server and handler up and running. As in the starter example I am using eventEmitter to trigger emit an event from a mutation and subscription that listens to that event. If I conditionally use wsLink || httpBatchLink depending by the window availability, it works as aspected. If I use splitLink base on condition op.type === "subscription" the eventEmitter is trigger but the event is lost without reaching the subscription procedure. I understand that using eventEmitter is not ideal, but still I would really appreciate if anybody could explain me the logic behind this behavior
6 Replies
oddiz
oddiz•13mo ago
Hey I'm struggling with the same problem. without using any external libraries when trying to use websocket I had to use splitLink to split wsLink and httpBatchLink. Which leads to events emitted from query and mutations to not reach subscription routes even though the event emitter is initialized at the root. Any solutions for this?
🟠 CHANCE
🟠 CHANCE•13mo ago
+1 -- Even without the use of event emitters, what is the recommended way to accomplish this? Solution: For now, you can just check if the window is present like OP mentions. It's not super ideal, but it works good enough and let's you keep moving. You will need to update your createTRPContext to work with both websockets and typical next requests.
Louis
Louis•13mo ago
I had an issue when tryign to follow tRPC's guide on subscriptions, passing createContext into the ws-server created the error: DiscordProvider is not a function. Did anyone manage to run a ws-server? Side note, Theo mentioned that he wanted to create a video tutorial using the app router that followed a similar pattern to his t3-next12 video.
🟠 CHANCE
🟠 CHANCE•13mo ago
The above solution will resolve your issue. Notably, it does introduce a few other caveats to work around, but it works in local and prod so probably good enough 🙂 @oddiz ^
🟠 CHANCE
🟠 CHANCE•13mo ago
reference repo that i used for testing: https://github.com/nftchance/siwe-next-auth-trpc-websockets
GitHub
GitHub - nftchance/siwe-next-auth-trpc-websockets
Contribute to nftchance/siwe-next-auth-trpc-websockets development by creating an account on GitHub.
oddiz
oddiz•13mo ago
hey thanks for the solution! I did kinda the same but I tried to merge the link of ws and trpc with splitLink. Also I didn't create the WsClient inside the same function as httpBatchLink it was on a seperate import. It must've been causing the issues. In the end i scraped the ws implementation maybe i'll try to add again later. this puts a nice perspective on the usage of createInnerContext and link stuff that I wasn't able to understand before. in the mean time hopefully your solution will guide some ppl since it does come up on google. https://github.com/nftchance/siwe-next-auth-trpc-websockets/blob/2ed6847cd03edf24c461f772b8ebb58b6d41e071/utils/api.ts#L29 this part is very important for whoever looking for solution
GitHub
siwe-next-auth-trpc-websockets/utils/api.ts at 2ed6847cd03edf24c461...
Contribute to nftchance/siwe-next-auth-trpc-websockets development by creating an account on GitHub.
Want results from more Discord servers?
Add your server