|
6 | 6 | A wrapper around the Quinn API, abstracting away the annoying HTTP/3 internals. |
7 | 7 | Provides a QUIC-like API but with web support! |
8 | 8 |
|
9 | | -## Example |
| 9 | +## WebTransport |
| 10 | +[WebTransport](https://developer.mozilla.org/en-US/docs/Web/API/WebTransport_API) is a new web API that allows for low-level, bidirectional communication between a client and a server. |
| 11 | +It's [available in the browser](https://caniuse.com/webtransport) as an alternative to HTTP and WebSockets. |
10 | 12 |
|
11 | | -See the example [server](examples/echo-server.rs) and [client](examples/echo-client.rs). |
| 13 | +WebTransport is layered on top of HTTP/3 which itself is layered on top of QUIC. |
| 14 | +This library hides that detail and exposes only the QUIC API, delegating as much as possible to the underlying QUIC implementation (Quinn). |
12 | 15 |
|
13 | | -QUIC requires TLS, which makes the initial setup a bit more involved. |
| 16 | +QUIC provides two primary APIs: |
14 | 17 |
|
15 | | -- Generate a certificate: `./cert/generate` |
16 | | -- Run the Rust server: `cargo run --example echo-server -- --tls-cert cert/localhost.crt --tls-key cert/localhost.key` |
17 | | -- Run a Web client: `cd web; npm install; npx parcel serve client.html --open` |
| 18 | +## Streams |
18 | 19 |
|
19 | | -If you get a certificate error with the web client, try deleting `.parcel-cache`. |
| 20 | +QUIC streams are ordered, reliable, flow-controlled, and optionally bidirectional. |
| 21 | +Both endpoints can create and close streams (including an error code) with no overhead. |
| 22 | +You can think of them as TCP connections, but shared over a single QUIC connection. |
20 | 23 |
|
21 | | -The Rust client example seems to be broken. |
22 | | -It would be amazing if somebody could fix it: `cargo run --example echo-client -- --tls-cert cert/localhost.crt` |
| 24 | +## Datagrams |
23 | 25 |
|
24 | | -## Limitations |
| 26 | +QUIC datagrams are unordered, unreliable, and not flow-controlled. |
| 27 | +Both endpoints can send datagrams below the MTU size (~1.2kb minimum) and they might arrive out of order or not at all. |
| 28 | +They are basically UDP packets, except they are encrypted and congestion controlled. |
25 | 29 |
|
26 | | -This library doesn't support pooling HTTP/3 or multiple WebTransport sessions. |
27 | | -It's means to be analogous to the QUIC API. |
| 30 | +# Usage |
| 31 | +To use web-transport-quinn, first you need to create a [quinn::Endpoint](https://docs.rs/quinn/latest/quinn/struct.Endpoint.html); see the documentation and examples for more information. |
| 32 | +The only requirement is that the ALPN is set to `web_transport_quinn::ALPN` (aka `h3`). |
28 | 33 |
|
29 | | -- If you want to support HTTP/3 on the same host/port, you should use another crate (ex. `h3-webtransport`). |
30 | | -- If you want to support multiple WebTransport sessions over the same QUIC connection... you should just dial a new QUIC connection instead. |
| 34 | +Afterwards, you use [web_transport_quinn::accept](https://docs.rs/web-transport-quinn/latest/web_transport_quinn/fn.accept.html) (as a server) or [web_transport_quinn::connect](https://docs.rs/web-transport-quinn/latest/web_transport_quinn/fn.connect.html) (as a client) to establish a WebTransport session. |
| 35 | +This will take over the QUIC connection and perform the boring HTTP/3 handshake for you. |
| 36 | + |
| 37 | +See the [examples](examples) or [moq-native](https://github.com/kixelated/moq-rs/blob/main/moq-native/src/quic.rs) for a full setup. |
| 38 | + |
| 39 | +```rust |
| 40 | + // Create a QUIC client. |
| 41 | + let mut endpoint = quinn::Endpoint::client("[::]:0".parse()?)?; |
| 42 | + endpoint.set_default_client_config(/* ... */); |
| 43 | + |
| 44 | + // Connect to the given URL. |
| 45 | + let session = web_transport_quinn::connect(&client, &"https://localhost").await?; |
| 46 | + |
| 47 | + // Create a bidirectional stream. |
| 48 | + let (mut send, mut recv) = session.open_bi().await?; |
| 49 | + |
| 50 | + // Send a message. |
| 51 | + send.write(b"hello").await?; |
| 52 | +``` |
| 53 | + |
| 54 | +## API |
| 55 | +The `web-transport-quinn` API is almost identical to the Quinn API, except that [Connection](https://docs.rs/quinn/latest/quinn/struct.Connection.html) is called [Session](https://docs.rs/web-transport-quinn/latest/web_transport_quinn/struct.Session.html). |
| 56 | + |
| 57 | +When possible, `Deref` is used to expose the underlying Quinn API. |
| 58 | +However some of the API is wrapped or unavailable due to WebTransport limitations. |
| 59 | +- Stream IDs are not avaialble. |
| 60 | +- Error codes are not full VarInts (62-bits) and significantly smaller. |
0 commit comments