Token Expiration Behavior

Hi @Alexander , I have noticed sometimes even though I might renew the access token, I still get disconnected from an existing socket connection. What is the best practice for when to renew? should I reconnect anytime my token expires?

Tue, 28 Dec 2021 10:54:50 -0800 - Any - Renewed Access token. Now expiring at 28-Dec-2021 12:14:47
Tue, 28 Dec 2021 11:09:07 -0800 - Trading - Received closing frame Some(
    CloseFrame {
        code: Normal,
        reason: "Bye",
    },
)

Probably the best way to handle this would be to listen for the close frame. Wrap your actual WebSocket instance in some an object, function, or class so that you can access the variable when you get the close frame. Typical socket implementations have the onclose handle to which you can assign a callback function, or listen for 'c' frames in the onmessage handle. When you get the close frame, renew your regular access token then create a new instance of the socket variable:

//in mySocket.onclose
mySocket = new WebSocket(URL)

mySocket.onopen = /* auth procedure */
mySocket.onmessage = /* message receipt */
mySocket.onclose = /* reconnect procedure */

Then re-run your authentication procedure with the new access token. This might result in a hiccup in your app’s service but it will probably be minimal. If you’re focusing on a particular market, you could minimize the effect further by performing this procedure during a time when that market is closed, or just before market open.