# Token Expiration Behavior

**URL:** <https://community.tradovate.com/t/token-expiration-behavior/3586>\
**Category:** Community Support\
**Created:** [December 28, 2021, 7:15pm UTC](https://community.tradovate.com/t/token-expiration-behavior/3586 "2021-12-28T19:15:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juan\_Marquez](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@Juan\_Marquez](https://community.tradovate.com/u/Juan_Marquez)\
**Post date:** [December 28, 2021, 7:15pm UTC](https://community.tradovate.com/t/token-expiration-behavior/3586/1 "2021-12-28T19:15:20Z")

</div>

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?

```auto
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",
    },
)

```

---

<div class="post-metadata">

**Author:** ![Alexander](https://sea1.discourse-cdn.com/flex015/user_avatar/community.tradovate.com/alexander/32/988_2.png) [@Alexander](https://community.tradovate.com/u/Alexander)\
**Post date:** [December 28, 2021, 9:04pm UTC](https://community.tradovate.com/t/token-expiration-behavior/3586/2 "2021-12-28T21:04:59Z")

</div>

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:

```auto
//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.
