Firefox Tomorrow

web api interface

WebTransportDatagramDuplexStream

View on MDN ↗

Secure context Available in workers

The WebTransportDatagramDuplexStream interface of the WebTransport API represents a duplex stream that can be used for unreliable transport of datagrams between client and server. Provides access to a ReadableStream for reading incoming datagrams, a WritableStream for writing outgoing datagrams, and various settings and statistics related to the stream.

This is accessed via the datagrams property.

“Unreliable” means that transmission of data is not guaranteed, nor is arrival in a specific order. This is fine in some situations and provides very fast delivery. For example, you might want to transmit regular game state updates where each message supersedes the last one that arrives, and order is not important.

Instance properties

Instance methods

Examples

Writing outgoing datagrams

This code uses the createWritable() method, if it is supported, to get a WebTransportDatagramsWritable instance that can be used for writing data to the transport. Otherwise, it falls back to the writable property Deprecated , which returns a WritableStream object that you can write data to using a writer instead:

const writableStream =
  typeof transport.datagrams.createWritable === "function"
    ? transport.datagrams.createWritable()
    : transport.datagrams.writable; // Deprecated and non-standard

const writer = writableStream.getWriter();
const data1 = new Uint8Array([65, 66, 67]);
const data2 = new Uint8Array([68, 69, 70]);
await writer.ready;
writer.write(data1);
await writer.ready;
writer.write(data2);

Reading incoming datagrams

The readable property returns a ReadableStream object that you can use to receive data from the server:

async function readData() {
  const reader = transport.datagrams.readable.getReader();
  while (true) {
    const { value, done } = await reader.read();
    if (done) {
      break;
    }
    // value is a Uint8Array.
    console.log(value);
  }
}

Specifications

SpecificationsStandards references are available on the canonical MDN page.

Browser compatibility

Browser compatibilityCompatibility data is available on the canonical MDN page.

See also