WebTransport

WebTransport is a network transport protocol based on HTTP/3. It provides bidirectional communication capabilities between clients and servers, balancing low latency, high throughput, and security.

Application Scenarios

WebTransport is particularly suitable for the following scenarios:

  • Real-time applications: Applications requiring low-latency communication, such as online games, real-time collaboration tools, and video conferencing.
  • Large file transfers: Supports high-throughput data transfer, suitable for media streaming and large file uploads/downloads.
  • Multiplexed communication: Allows establishing multiple bidirectional and unidirectional data streams simultaneously.
  • Datagram communication: Supports datagram communication where order and reliability are not guaranteed, suitable for scenarios with extremely high real-time requirements.

Compared to WebSocket, WebTransport offers lower latency and more flexible communication modes, performing particularly well in unstable network environments.

Salvo Implementation

Salvo framework provides built-in support for WebTransport, allowing developers to easily build WebTransport-based applications. Key features include:

  • Support for establishing WebTransport sessions
  • Support for Bidirectional Streams communication
  • Support for Unidirectional Streams communication
  • Support for Datagrams transmission
  • Server can initiate communication streams

Simple Example

Below is a simplified example of implementing a WebTransport server using Salvo:

#[handler]
async fn connect(req: &mut Request) -> Result<(), salvo::Error> {
    let session = req.web_transport_mut().await.unwrap();
    
    // Handle datagrams
    if let Ok(Some((_, datagram))) = session.accept_datagram().await {
        // Process the received datagram
        let mut resp = BytesMut::from(&b"Response: "[..]);
        resp.put(datagram);
        session.send_datagram(resp.freeze())?;
    }
    
    // Handle bidirectional streams
    if let Ok(Some(webtransport::server::AcceptedBi::BidiStream(_, stream))) = session.accept_bi().await {
        let (send, recv) = salvo::proto::quic::BidiStream::split(stream);
        // Process bidirectional stream data
    }
    
    Ok(())
}

Configuration and Startup

Starting a Salvo application with WebTransport support requires configuring TLS certificates and a QUIC listener:

let cert = include_bytes!("../certs/cert.pem").to_vec();
let key = include_bytes!("../certs/key.pem").to_vec();

// Configure routing
let router = Router::new().push(Router::with_path("counter").goal(connect));

// Configure TLS
let config = RustlsConfig::new(Keycert::new().cert(cert.as_slice()).key(key.as_slice()));

// Set up the listener
let listener = TcpListener::new(("0.0.0.0", 5800)).rustls(config.clone());
let acceptor = QuinnListener::new(config, ("0.0.0.0", 5800))
    .join(listener)
    .bind()
    .await;

// Start the server
Server::new(acceptor).serve(router).await;

Complete Example

To learn more about using WebTransport in Salvo, please refer to the complete example on GitHub: https://github.com/salvo-rs/salvo/blob/main/examples/webtransport

This example includes complete server-side and client-side implementations, demonstrating how to handle various types of WebTransport communication.