WebTransport()
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The WebTransport()
constructor creates a new
WebTransport
object instance.
Note: This feature is available in Web Workers
Syntax
new WebTransport(url, options)
Parameters
url
-
A string representing the URL of the HTTP/3 server to connect to. The scheme needs to be HTTPS, and the port number needs to be explicitly specified.
options
Optional-
An object containing the following properties:
serverCertificateHashes
Optional-
An array of
WebTransportHash
objects. If specified, it allows the website to connect to a server by authenticating the certificate against the expected certificate hash instead of using the Web public key infrastructure (PKI). This feature allows Web developers to connect to WebTransport servers that would normally find obtaining a publicly trusted certificate challenging, such as hosts that are not publicly routable, or ephemeral hosts like virtual machines.
WebTransportHash
objects contain two properties:
algorithm
-
A string representing the algorithm to use to verify the hash. Any hash using an unknown algorithm will be ignored.
value
-
A
BufferSource
representing the hash value.
Exceptions
NotSupportedError
DOMException
-
Thrown if
serverCertificateHashes
is specified but the transport protocol does not support this feature. SyntaxError
DOMException
-
Thrown if the specified
url
is invalid, if the scheme is not HTTPS, or if the URL includes a fragment.
Examples
const url = "https://example.com:4999/wt";
async function initTransport(url) {
// Initialize transport connection
const transport = new WebTransport(url);
// The connection can be used once ready fulfills
await transport.ready;
// ...
}
// ...
async function closeTransport(transport) {
// Respond to connection closing
try {
await transport.closed;
console.log(`The HTTP/3 connection to ${url} closed gracefully.`);
} catch (error) {
console.error(`The HTTP/3 connection to ${url} closed due to ${error}.`);
}
}
Specifications
Specification |
---|
WebTransport # dom-webtransport-webtransport |
Browser compatibility
BCD tables only load in the browser