Skip to content

Getting Started

André Borchert edited this page Sep 11, 2026 · 26 revisions

Getting Started

Requirements

  • macOS 26 or later
  • Xcode 26.6 or later
  • Swift 6.3.3 or later, using Swift language mode 6

The public integration point is the repository-root Package.swift.

Add the package

.package(
    url: "https://github.com/Pummelchen/WebTransport.git",
    exact: "1.3.5"
)

Add the WebTransport product to your application target. Lower-level products are available for protocol research and specialized integrations; most applications should start with the high-level product.

Connect a client

import Foundation
import WebTransport

let client = WebTransportClient(configuration: WebTransportClientConfiguration(
    authority: "example.com",
    path: "/wt",
    origin: "https://example.com",
    availableProtocols: ["demo.v1"]
))

let session = try await client.connect(
    to: WebTransportEndpoint(host: "example.com", port: 443)
)

let stream = try await session.openBidirectionalStream()
try await stream.send(Data("hello".utf8), endOfStream: true)
let response = try await stream.receive()
try await session.close()

Platform certificate trust is used by default.

Accept a session

import WebTransport

let server = WebTransportServer(configuration: WebTransportServerConfiguration(
    authority: "localhost",
    path: "/wt",
    supportedProtocols: ["demo.v1"],
    localOnly: true
))

let listener = try await server.listen(
    on: WebTransportEndpoint(host: "127.0.0.1", port: 4433)
)
defer { listener.shutdown() }

let session = try await listener.acceptSession()
let stream = try await session.acceptBidirectionalStream()
let request = try await stream.receive()
try await stream.send(request, endOfStream: true)

Server admission can enforce authority, path, origin, supported subprotocols, and HTTP/3 settings. The package does not provide application user authentication.

Supply a server TLS identity

Loopback development can use the generated self-signed certificate. Anything reachable by a real client needs a real identity, supplied in one of three ways:

Identity Use
.developmentSelfSigned Loopback only. Refused on any non-loopback bind.
.pkcs12(data:passphrase:) A PKCS#12 bundle holding the leaf certificate, its chain, and the private key. Usually the simplest path.
.certificateChain(chainDER:privateKeyDER:keyKind:) An explicit certificate chain plus a private key you already hold in Apple's representation.

Two encoding constraints that are easy to hit

Both stem from the same thing: this platform builds identities through Security.framework, which is stricter about key and certificate encoding than openssl is. Neither is a defect in the package, but both produce failures that are hard to read without knowing the cause.

1. The certificate must name its elliptic curve. macOS cannot build an identity from a certificate whose SubjectPublicKeyInfo carries explicit curve parameters — the full prime, coefficients, generator, order and cofactor — instead of a named-curve OID such as prime256v1.

This is easy to trip over because the system /usr/bin/openssl on macOS is LibreSSL 3.3.6, and LibreSSL writes the explicit form for an EC key. So the most obvious command on this platform produces a bundle that cannot be served:

# A self-signed EC certificate that will NOT import: LibreSSL writes explicit params.
/usr/bin/openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 \
  -keyout key.pem -out cert.pem -days 3650 -nodes -subj "/CN=example.com"
/usr/bin/openssl pkcs12 -export -out identity.p12 -inkey key.pem -in cert.pem -passout pass:pw

Importing that throws a WebTransportNetworkRuntimeError naming the cause — however the bundle is imported, it no longer terminates the process. Fix it by regenerating with a tool that emits a named curve (OpenSSL 3.x does by default), or by converting the key:

openssl ec -in key.pem -param_enc named_curve -out key-named.pem

RSA identities are unaffected, because there is no curve encoding to get wrong, which makes RSA the safe choice if you would rather not reason about this.

2. .certificateChain wants Apple's key representation, not DER. Despite the parameter name, privateKeyDER must hold the bytes SecKeyCopyExternalRepresentation returns for the private key. That is not the same bytes as the public key export: it is Apple's raw private form, 0x04, then X, then Y, then the private scalar, which makes it noticeably larger than the curve point.

Key Accepted length (private) Public export, for contrast
EC P-256 97 bytes 65 bytes
EC P-384 145 bytes 97 bytes
EC P-521 199 bytes 133 bytes
RSA-2048 ~1.2 KB (PKCS#1 RSAPrivateKey; varies by a few bytes) 270 bytes

A key in either openssl form is rejected, and the error message names the expected form and length so the mismatch is visible without a parser.

import Security

let attributes: [CFString: Any] = [
    kSecAttrKeyType: kSecAttrKeyTypeECSECPrimeRandom,
    kSecAttrKeySizeInBits: 256,
    kSecAttrIsPermanent: false,
]
guard let key = SecKeyCreateRandomKey(attributes as CFDictionary, nil),
    let privateKeyDER = SecKeyCopyExternalRepresentation(key, nil) as Data?
else { throw ... }

let identity = WebTransportServerIdentity.certificateChain(
    chainDER: [certificateDER],
    privateKeyDER: privateKeyDER,
    keyKind: .ellipticCurveP256
)

If your key is a PEM or DER file from openssl, converting it to a PKCS#12 bundle and using .pkcs12 is usually less work than reshaping it — provided the certificate names its curve, per constraint 1.

For reference, these are the encodings the platform rejects:

Input Result
SecKeyCopyExternalRepresentation output for the private key Accepted
The public key export (65 bytes for P-256) Rejected — it is not a private key
EC key, SEC1 (EC PRIVATE KEY) Rejected
EC key, PKCS#8 (PRIVATE KEY) Rejected
RSA key, PKCS#1 from openssl Rejected

See Known Limitations for the full boundary.

Run the command-line peers

Start a local IPv4 server:

swift run WebTransportServer --listen 127.0.0.1:4433 --transport packet

Connect from another terminal:

swift run WebTransportClient \
  --connect 127.0.0.1:4433 \
  --transport packet \
  --trust local-self-signed

Use [::1]:4433 for IPv6 loopback. Inspect all options and conformance scenarios with:

swift run WebTransportClient --help
swift run WebTransportServer --list

Real --listen and --connect sessions require packet transport. local-self-signed is explicit, development-only, and restricted to localhost, 127.0.0.1, and ::1.

Session operations

An established WebTransportSession provides:

  • bidirectional stream open and accept operations;
  • datagram send and receive operations when negotiated;
  • graceful drain and close;
  • selected subprotocol and endpoint metadata;
  • session-bound TLS exporter material.

Continue with Architecture, Development and Testing, and Security and Trust.

Clone this wiki locally