Skip to contentSkip to navigationSkip to topbar
On this page

Voice JS SDK v1: Twilio.PreflightTest


(warning)

Warning

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

1
import { Device, PreflightTest } from 'twilio-client';
2
3
const preflightTest = Device.runPreflight(token, options);
4
5
preflightTest.on(PreflightTest.Events.Completed, (report) => {
6
console.log(report);
7
});
8
9
preflightTest.on(PreflightTest.Events.Failed, (error) => {
10
console.log(error);
11
});

Method Reference

method-reference page anchor

.stop()

stop page anchor

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.


.on('completed', handler(report))

completed-handler page anchor

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",
3
4
/**
5
* The quality of the call, determined by the MOS (Mean Opinion Score) of the audio stream. Possible values include
6
* PreflightTest.CallQuality.Excellent - If the average mos is over 4.2
7
* PreflightTest.CallQuality.Great - If the average mos is between 4.1 and 4.2 both inclusive
8
* PreflightTest.CallQuality.Good - If the average mos is between 3.7 and 4.0 both inclusive
9
* PreflightTest.CallQuality.Fair - If the average mos is between 3.1 and 3.6 both inclusive
10
* PreflightTest.CallQuality.Degraded - If the average mos is 3.0 or below
11
*/
12
"callQuality": "excellent",
13
14
/**
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/RTCIceCandidateStats
18
*/
19
"iceCandidateStats": [...]
20
21
/**
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/RTCIceCandidateStats
25
*/
26
"selectedIceCandidatePairStats": {
27
"localCandidate": {...},
28
"remoteCandidate": {...}
29
}
30
31
/**
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,
38
39
/**
40
* Network related time measurements which includes millisecond timestamps
41
* 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 state
48
* https://developer.mozilla.org/en-US/docs/Web/API/RTCDtlsTransport/state.
49
*/
50
"dtls": {
51
"start": 1584573229981,
52
"end": 1584573230166,
53
"duration": 185
54
},
55
56
/**
57
* Measurements for establishing ICE connection.
58
* This is measured from ICE connection `checking` to `connected` state.
59
* See RTCPeerConnection.iceConnectionState
60
* https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/iceConnectionState.
61
*/
62
"ice": {
63
"start": 1584573229898,
64
"end": 1584573229982,
65
"duration": 84
66
},
67
68
/**
69
* Measurements for establishing a PeerConnection.
70
* This is measured from PeerConnection `connecting` to `connected` state.
71
* See RTCPeerConnection.connectionState
72
* https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/connectionState.
73
*/
74
"peerConnection": {
75
"start": 1584573229902,
76
"end": 1584573230167,
77
"duration": 265
78
},
79
80
/**
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.signalingState
85
* https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/signalingState
86
*/
87
"signaling": {
88
"start": 1595885835227,
89
"end": 1595885835573,
90
"duration": 346
91
}
92
},
93
94
/**
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": 35
103
},
104
"mos": {
105
"average": 2,
106
"max": 2,
107
"min": 2
108
},
109
"rtt": {
110
"average": 80.33,
111
"max": 88,
112
"min": 77
113
}
114
},
115
116
/**
117
* Timing measurement related to the test.
118
* Includes millisecond timestamps and duration.
119
*/
120
"testTiming": {
121
"start": 1584573229085,
122
"end": 1584573242279,
123
"duration": 13194
124
},
125
126
/**
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": 586
136
},
137
138
/**
139
* Array of samples collected during the test.
140
* See sample object format here
141
* https://www.twilio.com/docs/voice/sdks/javascript/v1/connection#sample
142
*/
143
"samples": [...],
144
145
/**
146
* The edge passed to `Device.runPreflight`.
147
*/
148
"selectedEdge": "roaming",
149
150
/**
151
* The edge that the test call was connected to.
152
*/
153
"edge": "ashburn",
154
155
/**
156
* PreflightTest.Warnings detected during the test.
157
*/
158
"warnings": [...]
159
}

.on('connected', handler())

connected-handler page anchor

Raised when PreflightTest.status has transitioned to PreflightTest.Status.Connected. This indicates that the connection to Twilio has been established.

.on('failed', handler(error))

failed-handler page anchor

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.

.on('sample', handler(sample))

sample-handler page anchor

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.

.on('warning', handler(warning))

warning-handler page anchor

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-sdk
5
*/
6
name: 'insights-connection-error',
7
8
/**
9
* Description of the Warning.
10
*/
11
description: 'Received an error when attempting to connect to Insights gateway',
12
13
/**
14
* Optional RTCWarning data coming from Connection.on('warning')
15
* See https://www.twilio.com/docs/voice/sdks/javascript/v1/connection#onwarning-handlerwarningname
16
*/
17
rtcWarning: {...}
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.

ValueDescription
CompletedThe connection to Twilio has been disconnected and the test call has completed.
ConnectedThe connection to Twilio has been established.
ConnectingConnecting to Twilio has started.
FailedThe test has stopped and failed.

TwiML Apps for PreflightTest

twiml-apps-for-preflighttest page anchor

Please see our example application(link takes you to an external page) 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(link takes you to an external page) 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"?>
2
3
<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(link takes you to an external page) 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"?>
2
3
<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(link takes you to an external page) 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"?>
2
3
<Response>
4
<Echo/>
5
</Response>

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.