You're viewing the 1.X version of the Voice JavaScript SDK (formerly called Twilio Client). Click here for information on how to migrate to the 2.X version.
A Twilio.PreflightTest
object represents a test call to Twilio which provides information to help troubleshoot call related issues. You never instantiate it directly, but it's returned when you call Twilio.Device.runPreflight(token, options).
1import { Device, PreflightTest } from 'twilio-client';23const preflightTest = Device.runPreflight(token, options);45preflightTest.on(PreflightTest.Events.Completed, (report) => {6console.log(report);7});89preflightTest.on(PreflightTest.Events.Failed, (error) => {10console.log(error);11});
Calling this method from the PreflightTest
object will stop the existing test and will raise a failed
event with an error code 31008
indicating that the call has been cancelled.
Raised when PreflightTest.status
has transitioned to PreflightTest.Status.Completed
. During this time, the report
is available and ready to be inspected. This will not trigger if a fatal error is encountered during the test. Example report:
1{2"callSid": "CAa6a7a187a9cba2714d6fdccf472cc7b1",34/**5* The quality of the call, determined by the MOS (Mean Opinion Score) of the audio stream. Possible values include6* PreflightTest.CallQuality.Excellent - If the average mos is over 4.27* PreflightTest.CallQuality.Great - If the average mos is between 4.1 and 4.2 both inclusive8* PreflightTest.CallQuality.Good - If the average mos is between 3.7 and 4.0 both inclusive9* PreflightTest.CallQuality.Fair - If the average mos is between 3.1 and 3.6 both inclusive10* PreflightTest.CallQuality.Degraded - If the average mos is 3.0 or below11*/12"callQuality": "excellent",1314/**15* An array of WebRTC stats for the ICE candidates gathered when connecting to media.16* Each item is an RTCIceCandidateStats object which provides information related to an ICE candidate.17* See RTCIceCandidateStats for more details https://developer.mozilla.org/en-US/docs/Web/API/RTCIceCandidateStats18*/19"iceCandidateStats": [...]2021/**22* A WebRTC stats for the ICE candidate pair used to connect to media, if candidates were selected.23* Each item is an RTCIceCandidateStats object which provides information related to an ICE candidate.24* See RTCIceCandidateStats for more details https://developer.mozilla.org/en-US/docs/Web/API/RTCIceCandidateStats25*/26"selectedIceCandidatePairStats": {27"localCandidate": {...},28"remoteCandidate": {...}29}3031/**32* Whether a TURN server is required to connect to media.33* This is dependent on the selected ICE candidates, and will be true if either is of type "relay",34* false if both are of another type, or undefined if there are no selected ICE candidates.35* See `PreflightTest.Options.iceServers` for more details.36*/37"isTurnRequired": false,3839/**40* Network related time measurements which includes millisecond timestamps41* and duration for each type of connection.42*/43"networkTiming": {44/**45* Measurements for establishing DTLS connection.46* This is measured from RTCDtlsTransport `connecting` to `connected` state.47* See RTCDtlsTransport state48* https://developer.mozilla.org/en-US/docs/Web/API/RTCDtlsTransport/state.49*/50"dtls": {51"start": 1584573229981,52"end": 1584573230166,53"duration": 18554},5556/**57* Measurements for establishing ICE connection.58* This is measured from ICE connection `checking` to `connected` state.59* See RTCPeerConnection.iceConnectionState60* https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/iceConnectionState.61*/62"ice": {63"start": 1584573229898,64"end": 1584573229982,65"duration": 8466},6768/**69* Measurements for establishing a PeerConnection.70* This is measured from PeerConnection `connecting` to `connected` state.71* See RTCPeerConnection.connectionState72* https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/connectionState.73*/74"peerConnection": {75"start": 1584573229902,76"end": 1584573230167,77"duration": 26578},7980/**81* Measurements for establishing Signaling connection.82* This is measured from initiating a connection using `device.connect()`,83* up to when RTCPeerConnection.signalingState transitions to `stable` state.84* See RTCPeerConnection.signalingState85* https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/signalingState86*/87"signaling": {88"start": 1595885835227,89"end": 1595885835573,90"duration": 34691}92},9394/**95* RTC related stats that are extracted from WebRTC samples.96* This information includes maximum, minimum, and average values calculated for each stat.97*/98"stats": {99"jitter": {100"average": 35,101"max": 35,102"min": 35103},104"mos": {105"average": 2,106"max": 2,107"min": 2108},109"rtt": {110"average": 80.33,111"max": 88,112"min": 77113}114},115116/**117* Timing measurement related to the test.118* Includes millisecond timestamps and duration.119*/120"testTiming": {121"start": 1584573229085,122"end": 1584573242279,123"duration": 13194124},125126/**127* Calculated totals in RTC statistics samples.128*/129"totals": {130"bytesReceived": 62720,131"bytesSent": 93760,132"packetsLost": 0,133"packetsLostFraction": 0,134"packetsReceived": 392,135"packetsSent": 586136},137138/**139* Array of samples collected during the test.140* See sample object format here141* https://www.twilio.com/docs/voice/sdks/javascript/v1/connection#sample142*/143"samples": [...],144145/**146* The edge passed to `Device.runPreflight`.147*/148"selectedEdge": "roaming",149150/**151* The edge that the test call was connected to.152*/153"edge": "ashburn",154155/**156* PreflightTest.Warnings detected during the test.157*/158"warnings": [...]159}
Raised when PreflightTest.status
has transitioned to PreflightTest.Status.Connected
. This indicates that the connection to Twilio has been established.
Raised when PreflightTest.status
has transitioned to PreflightTest.Status.Failed
. This happens when establishing a connection to Twilio has failed or when a test call has encountered a fatal error. This is also raised if PreflightTest.stop
is called while the test is in progress. The error emitted from this event is coming from Device.on('error') and uses the same error format.
This event is published every second and is raised when the Connection gets a webrtc sample object. The sample
object is coming from Connection.on('sample') and uses the same sample
format.
Raised whenever the test encounters a warning. Example warning data:
1{2/**3* Name of the warning.4* See https://www.twilio.com/docs/voice/insights/call-quality-events-twilio-client-sdk5*/6name: 'insights-connection-error',78/**9* Description of the Warning.10*/11description: 'Received an error when attempting to connect to Insights gateway',1213/**14* Optional RTCWarning data coming from Connection.on('warning')15* See https://www.twilio.com/docs/voice/sdks/javascript/v1/connection#onwarning-handlerwarningname16*/17rtcWarning: {...}18}
The callsid generated for the test call. This is set when the client has finished connecting to Twilio.
A timestamp in milliseconds of when the test ended. This is set when the test has completed
and raised the completed event.
The latest WebRTC sample collected. This is set whenever the connection emits a sample
. Please see Connection.on('sample') API for more details.
The report for this test. This is set when the test has completed and raised the completed
event.
A timestamp in milliseconds of when the test started. This is set right after calling Device.runPreflight(token, options)
.
The status of the test. Below are the possible values for this property.
Value | Description |
---|---|
Completed | The connection to Twilio has been disconnected and the test call has completed. |
Connected | The connection to Twilio has been established. |
Connecting | Connecting to Twilio has started. |
Failed | The test has stopped and failed. |
Please see our example application if you don't want to setup the required TwiML Apps manually.
Twilio.Device.runPreflight(token, options) requires a Twilio Access Token to initiate the test call. This access token will be passed directly to the Device's constructor and will be used to connect to a TwiML app that you associated with your Twilio Access Token. In order to get better results, the TwiML app should be able to record audio from a microphone and play it back to the browser. Below are example TwiML Apps that you can use and some setup instructions.
If PreflightTest.Options.fakeMicInput
is set to false
, Device.runPreflight(token, options)
API requires a token with a TwiML app that can record an audio from a microphone and the ability to play the recorded audio back to the browser. In order to achieve this, we need two TwiML endpoints: one to capture and record the audio, and another one to play the recorded audio.
In this example, we will use TwiML Bins for our TwiML app. Start by going to the TwiML Bin page in the Twilio Console.
Create a new TwiML Bin with the plus button on that screen and use Playback
as the friendly name. Then use the following template under the TwiML section.
1<?xml version="1.0" encoding="UTF-8"?>23<Response>4<Say>You said:</Say>5<Play loop="1">{{RecordingUrl}}</Play>6<Say>Now waiting for a few seconds to gather audio performance metrics.</Say>7<Pause length="3"/>8<Say>Hanging up now.</Say>9</Response>
Using the TwiML Bin page, let's create another TwiML Bin by clicking the plus button on that screen and use Record as the friendly name. Then replace the action URL in the following template with your TwiML Bin's Playback URL that you created previously. Let's use this template under the TwiML section.
1<?xml version="1.0" encoding="UTF-8"?>23<Response>4<Say>Record a message in 3, 2, 1</Say>5<Record maxLength="5" action="https://my-record-twiml-url"></Record>6<Say>Did not detect a message to record</Say>7</Response>
Now that we have created our TwiML Bins, let's create our TwiML app by going to the TwiML Apps page. Click the plus button on that screen and enter a friendly name that you prefer. Under Voice request URL, enter the TwiML Bin's Record URL that you created in the previous section, and then click the Create button.
On that same page, open the TwiML app that you just created by clicking on it and make note of the SID. You can now use this TwiML app to generate your token when calling Device.runPreflight(token, options) API.
If PreflightTest.Options.fakeMicInput
is set to true
, Device.runPreflight(token, options)
API requires a token with a single TwiML app that can capture and play an audio. Following the previous steps, create a TwiML Bin and a TwiML App using the following template.
1<?xml version="1.0" encoding="UTF-8"?>23<Response>4<Echo/>5</Response>