Chenna
Chenna

Reputation: 2623

Twilio Re-Initialize JS Client when JWT token expires

The capability token is set to expire in 60 seconds, so when it is expired I'm trying to re-setup the device

twilioDevice = new Twilio.Device();

// I also need incoming calls too, so client has to be refreshed every 60 sec
this.twilioDevice.on('error', error => {
   if (error.code === 31205) {
     this.initilizeTwilio();
   } else {
     this.toastr.warning(error.code + '' + error.message);
   }
});

initilizeTwilio() {
    this.twilioService.getCapability(60).subscribe(data => {
       if (this.twilioDevice.isInitialized) {
           this.twilioDevice.setup(data.capabilitytoken);
       } else {
          this.twilioDevice.setup(data.capabilitytoken, {debug: true});
       }
 }

For the first time setup work fine, when token is expired .setup() called, is there any other method which needs to invoked? What am I doing wrong here?

Image

Twilio Reference

Upvotes: 0

Views: 371

Answers (1)

philnash
philnash

Reputation: 73055

Twilio developer evangelist here.

I added it as a comment, but I wanted to call out the answer here.

In the timestamps in your image, you create the device and set it up the first time, you then get the error 60 seconds later (as expected) but also 62 seconds later. So it looks as though there is a caching issue on your server side.

If you are looking to take incoming calls, you might consider lengthening the timeout for your access token too.

Upvotes: 1

Related Questions