Fouad Raheb
Fouad Raheb9mo ago

How to have 2 separate trpc clients for http and websockets?

I'm trying to have a setup where I can use trpc with 2 separate clients, one for normal HTTP requests and one for websockets since most of our site pages do not need a websocket connection. I have tried to create 2 files with 2 different createTRPCNext clients, it works fine in pages that don't use useSubscription but when using it, it looks like only the first trpc client that was initialized will be used, because even if I'm using the websocket client for useSubscription, I'm getting this error TRPCClientError: Subscriptions should use wsLink
8 Replies
Nick
Nick9mo ago
Have a look at our SOA example One client, many backend
Fouad Raheb
Fouad Raheb9mo ago
Can you please share the SOA example link? I couldn't find it on the site and github, sorry Found it! https://github.com/trpc/trpc/tree/main/examples/soa It didn't help in my case with websockets I'm trying to have this example use 2 separate trpc clients, so that only some queries/mutations/subscriptions use the websocket client and the rest with the default http client https://github.com/trpc/examples-next-prisma-websockets-starter
Nick
Nick9mo ago
Not 100% sure but isn't the splitLink exactly what you want for that? SOA uses it already I believe
Fouad Raheb
Fouad Raheb9mo ago
I tried using splitLink and requests started going through http but a websocket connection is still starting as showing in dev tools with an empty array message []
No description
Nick
Nick9mo ago
Hm, I think @julius might be our resident websockets and soa pro then
Fouad Raheb
Fouad Raheb9mo ago
Seems like the only way to not have an empty websocket connection created is by creating wsLink on run time, but this would create multiple connections for each useSubscription. We just need to have a way to not start a connection if no messages are sent yet and no active useSubscription
(runtime) => {
return (ctx) => {
if (ctx.op.type === "subscription") {
return getWSLink()(runtime)(ctx);
} else {
return getBatchLink()(runtime)(ctx);
}
};
},
(runtime) => {
return (ctx) => {
if (ctx.op.type === "subscription") {
return getWSLink()(runtime)(ctx);
} else {
return getBatchLink()(runtime)(ctx);
}
};
},
julius
julius9mo ago
i don't think I've ever touched those the websocket parts - just been fighting our example to work with newer next versions haha
Fouad Raheb
Fouad Raheb9mo ago
Haha good luck
More Posts
How to configure context for a standalone server?Are there any examples out there on how to set up context for a standalone server? The docs do incluDocs hard to follow with so much `next` codeNot having a great experience with the docs. There are many examples that use `next`, making it hardadding information to the QueryKey that is not part of the procedure inputwe have the situation that we have our trpc routes read some specific information from the browsesr How to make typed error responses in the context of a specific query?Each query has a typed (success) response, although it seems all queries have a shared type for the What's the negative code returned in error responses?What's the negative error code returned in errors responses and how is it meant to be used by the clTRPC File as InputHi can someone pls help me: https://stackoverflow.com/questions/77381516/nextjs-trpc-file-as-input`refetchOnWindowFocus` with RSC?Hi. Is there any way to have `refetchOnWindowFocus` work with server components? Seems like it onlyMulti useSubscription with trpc react -query integration failed to find second subusing trpc in nx monorepo, node version is 20 with yarn. backend is using trpc with express integratIs there any easy way to create a Type for a trpc routerI want to publish a set of trpc api client to npm, which will be used by my customer directly. But iTRPC with Nextjs 13 App RouterIm trying to implement TRPC with my already existing Nextjs 13 App to get typesafe API's. I've manag