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 interface of the WebTransport API provides functionality to enable a user agent to connect to an HTTP/3 server, initiate reliable and unreliable transport in either or both directions, and close the connection once it is no longer needed.

Note: This feature is available in Web Workers

Constructor

WebTransport() Experimental

Creates a new WebTransport object instance.

Instance properties

closed Read only Experimental

Returns a promise that resolves when the transport is closed.

datagrams Read only Experimental

Returns a WebTransportDatagramDuplexStream instance that can be used to send and receive datagrams.

incomingBidirectionalStreams Read only Experimental

Represents one or more bidirectional streams opened by the server. Returns a ReadableStream of WebTransportBidirectionalStream objects. Each one can be used to read data from the server and write data back to it.

incomingUnidirectionalStreams Read only Experimental

Represents one or more unidirectional streams opened by the server. Returns a ReadableStream of WebTransportReceiveStream objects. Each one can be used to read data from the server.

ready Read only Experimental

Returns a promise that resolves when the transport is ready to use.

Instance methods

close() Experimental

Closes an ongoing WebTransport session.

createBidirectionalStream() Experimental

Opens a bidirectional stream; returns a WebTransportBidirectionalStream object containing readable and writable properties, which can be used to read from and write to the server.

createUnidirectionalStream() Experimental

Opens a unidirectional stream; returns a WritableStream object that can be used to write to the server.

Examples

The following snippet shows how you'd connect to an HTTP/3 server by passing its URL to the WebTransport() constructor. Note that the scheme needs to be HTTPS, and the port number needs to be explicitly specified. Once the WebTransport.ready promise fulfills, you can start using the connection.

Also note that you can respond to the connection closing by waiting for the WebTransport.closed promise to fulfill. Errors returned by WebTransport operations are of type WebTransportError, and contain additional data on top of the standard DOMException set.

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}.`);
  }
}

For other example code, see the individual property and method pages.

Specifications

Specification
WebTransport
# web-transport

Browser compatibility

BCD tables only load in the browser

See also