twelventi
twelventi11mo 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
twelventiOP11mo ago
Thanks in advance for any help!!!
BeBoRE
BeBoRE11mo 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
itsnotyalc9mo ago
Following this as I'm having the same issue @twelventi - did you figure this out yet?
twelventi
twelventiOP9mo 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
itsnotyalc9mo ago
Do you mind sharing that pattern? I just tried this approach out but no dice
twelventi
twelventiOP9mo 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
itsnotyalc9mo ago
Hmm, okay. Appreciate the response!