Token expiration in Twilio - 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?

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.

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.

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)
})
})

Related

QuickFIX/J - how to handle End Of Stream

I am new to QuickFIX/J.
Creating initiator using websocket(frontend-angular, backend-Springboot websocket). Using w.3.
I would like to handle session expired issue. When the FIX server session is expired, it is sending Logout with reason session deactivated. That works fine for already connected connection/session.
Now, after this, trying to initiate connection, it keeps calling back logout and toAdmin with repeating event/error on screen log (as event) "Disconnecting, End Of Stream encountered".
I would like to handle this scenario and want to capture this in my code so that proper message to UI will be sent.
I don't know which QuickFIX/J version you are using, but on versions up to 2.2.0 you could implement the quickfix.SessionStateListener in your application and utilize its onDisconnect() callback.
If you are even using version 2.3.0 you could also use onConnectException() which will handle some additional scenarios that the first callback does not cover.

Twilio Ougoing Voice Call : Token does not allow outgoing calls on first attempt

I have recently started experiencing peculiar behaviour trying to initiate an outgoing phone call Browser -> Phone.
Every time I refresh the browser, my initial call to Twilio.Device.connect(phonecallParams) results in an error:
Received an error from the gateway: {code: 31002, connection: Connection, message: "Token does not allow outgoing calls."}.
If I make a second call to Twilio.Device.connect(phonecallParams), it works.
Also every subsequent call works. But if I refresh the browser then the first call fails again.
It used to work first time, every time. But I last tested this weeks ago.
Now it fails first time, every time.
With the first, failed call:
I get call my back-end to get a token
I return the token to the JS method
I call Twilio.Device.connect(phonecallParams)
I get the error message.
Nothing else happens. There is no attempt to call my TwiML handler.
With the second, successful call:
I get call my back-end to get a token
I return the token to the JS method
I call Twilio.Device.connect(phonecallParams)
My backend method that handles TwiML is called
Call is initiated.
In both cases:
the token construction is identical the same method which creates the token the same way (and includes new OutgoingClientScope(_twilioAccount.TwiMLApplicationSid))
the phonecallParams are identical
Would anyone have a clue as to what could be going on??
Ok, so this turned out to be one of those silly things...
I was calling Twilio.Device.connect() immediately / too soon after requesting my token - the same trigger that requested the token also triggered Twilio.Device.connect().
I now request my outgoing phone token when my page loads, and by the time the user wants to initiate the call the token has been completely set up, wherever it needs to be.

CallKit error com.apple.CallKit.error.requesttransaction error 7

I'm using Twilio voice quickstart code https://github.com/twilio/voice-quickstart-swift.
When I make a client to client call, call doesn't connect. CallKit runs in the background though, I can see the green notification bar when I send app in the background.
Following is the error:
StartCallAction transaction request failed: The operation couldn’t be completed. (com.apple.CallKit.error.requesttransaction error 7.)
As you can see Googling doesn't help as there doesn't seem to be any solution around?
Does anyone know how to fix or debug it further?
Updated:
Attaching VoIP settings, it's certainly enabled.
Problem is in your code which you write to handle and initialise variables. There is nothing wrong in the Twilio sdk either so don't look there. Anything which you are doing beyond twilio sample code is the place to look for the problem.
I've also wasted months of my time on similar issue and found out that there was issue with initialising one variable.
You are trying to request CXStartCallAction right after another CXStartCallAction was requested. You need to end the first call correctly.
In any case you must follow correct sequence of actions. Once you user wrong action in a sequence, CallKit will return one or another error.
And DO NOT request one action immediately after another is processed. There should be some time between two requests. For example, you initiated CXStartCallAction, then you checked that user is offline and trying to end the call. If that check is quick, then "end action" may result in error. You need to wait a few milliseconds before cancelling the outgoing call.
Twilio developer evangelist here.
Have you enabled capabilities for Voice over IP in the project settings?
Try to initialize CXProvider and CXCallController sooner, before requesting CXStartCallAction
I had the same problem because the Provider and the CallController have been lazy loaded.
It looks like that the CXProvider initWithConfiguration runs asynchronously which means you need to call this early otherwise you run into the risk of having a call without the completion of the initWithConfiguration function.
Thanks to #Allen for pointing me in the right direction.

When to authenticate to server in iOS app

I have an iOS app that authenticates to a remote API. The server gives back a token that is used for all the next requests. The authentication call is a simple POST to /api/auth.
My question is: where should I make this call in the app ?
I don't know if I should use it in the AppDelegete (willEnterForeground or didBecomeActive), because it may slow down the app launch. Moreover, this is asynchronous and if I try to make other requests in some controllers while the token hasn't returned, there will be errors.
So I thought about doing it in the root controller, but in the case the app was in the background for a long time and comes to foreground in another controller it doesn't work...
The last option would be to watch errors on every call, and re-authenticate when the server responds with a 'token expired' error. In that case I should probably have a special class for HTTP requests and error handling ?
I don't know what option is the best...

Twilio SDK General error #31000

We use Twilio SDK in our iOS app. It works fine but sometimes didStopListeningForIncomingConnections callback is called with error=31000 ("General error"). After that, the device turns to a strange state: it seems to be online but it's impossible to call it. And it shows "unconnected" state on the device.
So the questions are:
1. What does this 31000 error means?
2. What should we do in such a case? How to reconnect device to Twilio?
Megan from Twilio here.
You can see what an error for Twilio Client means here: https://www.twilio.com/docs/api/client/errors
However, 31000 is a rather vague and less than ideal error message as you describe. In this case, it is likely that the Twilio capability token has probably expired while the application is in the background, and if you merely call the listen method whenever they are receiving the 31000 generic error, it might cause the client SDK to result in a error-retry loop and crash the application eventually.
At the time of your writing with TwilioClient iOS SDK v1.2.5, it is suggested to use the following sample code in your did-stop-listening callback:
- (void)device:(TCDevice*)device didStopListeningForIncomingConnections:(NSError*)error {
if ( [self checkCapabilityTokenStillValid] ) {
// if the token has not yet expired, use the `listen` method of `TCDevice` to resume the listening state
[self.device listen];
}
else {
// restart all over by requesting a new capability token and initialize/update the `TCDevice` instance again
[self login];
}
}
The TwilioClient iOS SDK takes care of dispatching the listen and updateCapabilitiyToken: methods to the current thread for execution, therefore it's safe to call them directly in the didStopListeningForIncomingConnections. The did-stop-listening delegate method is always triggered with dispatch_get_main_queue() of Grand Central Dispatch.
Hope this may help others if they run into the same generic error.
This may or may not be the issue, we have encountered 31000 errors two times in our development and both were a result of generating the JWT on our server api. To be clear the error was a 31000 on the client, but the reason for this was in the construction of the JWT, and the params we wanted twilio to send back to our application.
When passing in an object to allow_client_outgoing or allow_client_incoming the twilio sdk concats this all in their scope attribute in their JWT. It added it to the scope:client:outgoing?appSid= which looks like a query string. That means it has a size limit of 2048. So exceeding this length generates a 31000 error.
In addition adding the objects doesn't seem to always implicitly serialize the object correctly, it introduces characters that can generate errors in their corresponding mobile sdks (but not their web sdk ... weird) so we took care of this by explicitly serializing objects to JSON before they are inserted into the JWT.
I hope both of these examples help you track down the issue.

Resources