What it measures
Packet loss is the ratio of UDP packets sent to a TURN server that never return, expressed as a value between0 (no loss) and 1 (total loss). A ratio of 0.02, for example, means 2% of packets were lost.
This metric is a direct signal of connection reliability. High packet loss degrades real-time applications such as video calls, gaming, and VoIP.
How it works
The engine opens a WebRTC data channel to a TURN server and sends a configurable number of UDP packets in a round-trip fashion. Each packet that is sent is expected to return. After all packets have been sent, the engine waits a configurable amount of time for any late-arriving responses. Packets that never return are counted as lost. Packets are sent in batches to avoid flooding the connection. The batch size and inter-batch wait time are both configurable.Requirements
Packet loss measurement requires a TURN server that the browser can reach over UDP. You must supply either:- A credentials API URL (
turnServerCredsApiUrl) pointing to an endpoint that returns short-lived credentials, or - A static username and password (
turnServerUser/turnServerPass).
Constructor options
These options are passed when instantiatingSpeedTest. They configure which TURN server to connect to and how to authenticate.
URI of the TURN server used for packet loss measurement. Provide your own server URI when the public one is deprecated.
URL of an endpoint that returns short-lived TURN credentials as JSON. The response must include
username and credential string fields, and may optionally include a urls array of TURN server addresses. Takes precedence over static credentials when provided. See TURN server setup for a reference implementation.Static TURN server username. Use when you are not using a credentials API.
Static TURN server password. Use when you are not using a credentials API.
Measurement configuration
ThepacketLoss measurement type accepts the following fields inside the measurements array:
Must be
"packetLoss".Total number of UDP packets to send. The default sequence uses
1000.Milliseconds to wait after the last packet reception before closing the measurement and counting unreturned packets as lost. The default sequence uses
3000 ms.Number of packets per batch. If higher than
numPackets, all packets are sent in a single batch.Milliseconds to wait between batches.
Milliseconds to wait for a successful TURN connection before aborting.
Default sequence entry
The default measurement sequence includes the following packet loss entry:defaultConfig.js
measurements array in the constructor.
Basic setup
Reading results
getPacketLoss()
Returns the packet loss ratio as a number between 0 and 1, or undefined if no packet loss measurement has completed.
getPacketLossDetails()
Returns the full detail object for the packet loss measurement, or undefined if no measurement has completed.
On success, the shape is:
Listening for changes
UseonResultsChange to act on the packet loss result as soon as it is available:
Error handling
If the engine cannot connect to the TURN server, or if credential fetching fails, theonError callback is invoked with a string describing the failure. The getPacketLossDetails() return value will contain an error field instead of measurement data.
- The TURN server is unreachable from the client’s network.
- The credentials endpoint returned an unexpected status or malformed JSON.
- The connection timed out before the TURN handshake completed (see
connectionTimeout).
