twelventi
twelventi7mo ago

need help refreshing websocket

We currently are using tRPC w/ react and websockets. We’re using the URL of the websocket as the auth token to the websocket I.e. ws://localhost?token=12345 We want to be able to kill this websocket connection and reopen one with a new token, I.e. ws://localhost?token=12346 I’ve tried putting the trpcClient in react state, but when the state updates the old websocket connections stay open. I’ve also tried setting the URL with a function, and manually killing the websocket, and letting the WS automatically reconnect, but there’s no way to manually kill the websocket from the client Does anyone have advice or could like a pattern maybe where the websocket url has to dynamically change. (Or has a better way to continually authenticate a websocket connection)
7 Replies
twelventi
twelventi7mo ago
Thanks in advance for any help!!!
BeBoRE
BeBoRE6mo ago
Pretty sure if you close the websocket connection it will reopen it So you could use that as a side effect when your access code changes
itsnotyalc
itsnotyalc5mo ago
Following this as I'm having the same issue @twelventi - did you figure this out yet?
twelventi
twelventi5mo ago
@itsnotyalc I believe when you create a wslink, and a ws client, you can pass a function to the url parameter which can dynamically return a URL, but we ended up not going with a pattern that required a new url for the websocket everytime so I’m not sure if that works I.e.
createWsClient({
url: () => dynamicFunctionToGetUrl()
})
createWsClient({
url: () => dynamicFunctionToGetUrl()
})
itsnotyalc
itsnotyalc5mo ago
Do you mind sharing that pattern? I just tried this approach out but no dice
twelventi
twelventi5mo ago
We pass a session token to the websocket that is basically a lookup in a diff place to see if that session is still active Then re-authenticating keeps that same session ID alive
itsnotyalc
itsnotyalc5mo ago
Hmm, okay. Appreciate the response!
More Posts
router 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. DiThis page needs to bail out of prerendering at this point because it used revalidate: 0I've been testing out partial prerendering with next using tRPC but having some issues. Everytime How to infer query types on client?```ts export const tenantRouter = router({ getTenants: procedure.query(() => { return { hello:NextJS Output TypingsQuick question probably stupid, I learned about this library yesterday and completely migrated from