user2845617
user2845617

Reputation: 61

Token expiration in Twilio

I'm working on embedding a soft phone into a web page that will go into Odoo (web based ERP system). It will allow inbound and outbound calls for employees.

The token expires every hour. So this means the user will have to refresh the page every hour. I could do an http refresh but if the user is on a call when it does the refresh it will knock them off the call.

How do we get around this so we can build a fully working dialer?

Upvotes: 2

Views: 1432

Answers (3)

siannopollo
siannopollo

Reputation: 1466

I just ran into this issue so hopefully my solution can help you and others.

I was using twilio.js v1.3 and tried implementing my offline callback like @philnash recommended, but kept getting the error device.setup is not a function. I then tried using Twilio.Device.setup(newToken) and was able to get the capability token refreshed but also ended up getting a new error: Cannot read property 'setToken' of undefined.

I ended up having to use twilio.js v1.4 to make the error go away. My working solution looks like this:

Twilio.Device.offline(function(device) {
  $.ajax(urlToGetNewToken, type: 'get').done(function(newToken) {
    Twilio.Device.setup(newToken)
  })
})

Upvotes: 0

philnash
philnash

Reputation: 73055

Another Twilio evangelist here!

You can actually listen for the offline event on the Twilio.Device object. From the documentation:

.offline( handler(device) )

Register a handler function to be called when the offline event is fired. This is triggered when the connection to Twilio drops or the device's capability token is invalid/expired. In either of these scenarios, the device cannot receive incoming connections or make outgoing connections. If the token expires during an active connection the offline event handler will be called, but the connection will not be terminated. In this situation you will have to call Twilio.Device.setup() with a valid token before attempting or receiving the next connection.

So you want something like:

Twilio.Device.offline(function(device) {
  fetchTokenFromServer(function(token) {
    device.setup(token);
  });
});

where fetchTokenFromServer makes the HTTP request that Devin suggested in his answer.

Let me know if this helps.

Upvotes: 2

Devin Rader
Devin Rader

Reputation: 10366

Twilio evangelist here.

I'd suggest using JavaScript to do an asynchronous HTTP request to get a new token from your server and then updating the instance of client with it.

Hope that helps.

Upvotes: 2

Related Questions