What is a good way for a client application to know when its Twilio Capability Token has expired?
In my case, the client application is a Javascript application running inside a web browser.
The reason I ask is because I want to make sure that the client application (running in the web browser) is always ready to receive incoming calls that are intended for it. It occurs to me that if the Capability Token that the client application is using has expired, then calls to that application will probably fail without its knowledge.
The best solution I can thinking of so far is for the client application to keep track of the expiration on the Capability Token and automatically query the server for a new one shortly before it expires. (Using Javascript's setTimeout()
?
More ideally would be if Twilio.Device
in the Javascript library had a callback that could be set for when the token was about to expire. That way the client could listen to that can request a new token at that time.
Another solution could possibly be for the server to be able to extend the expiration of the client's Capability Token if it knows that a call has come into that is intended for that client. However this is functionality that Twilio may or may not provide.
What is a good way to handle this?
The technique you mentioned using setTimeout() is a completely valid way to manage this. Since your application sets the expiry time, you can set a future function call that coincides to request a new token.
Alternately, if you prefer a callback you can implement Twilio.Device's offline handler. This is called when the token expires, or the Internet connection drops. Either way you'll need a new token to re-establish your offline Client's connection to Twilio.
You can read more about Twilio.Device callbacks in the documentation or Skills Training.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With