qmux

An Erlang implementation of QMux (draft-ietf-quic-qmux-02): QUIC's streams, flow control, stream limits and datagrams carried over a reliable byte stream such as TLS over TCP, or over WebSocket messages (draft-lcurley-qmux-websocket-00). Applications built on QUIC streams can use it as a fallback for networks that block UDP. Its API and events mirror erlang_quic's, so an application can drive native QUIC and QMux the same way.

It needs only OTP (Erlang/OTP 29 or later). TrevRPC uses it for its TCP fallback. TrevRPC's interop suite runs its bidirectional streams against the qmux crate and TrevRPC's Go and JavaScript implementations, and all of them are checked against the same golden vectors. Unidirectional streams and datagrams are covered by this package's own tests only.

%% rebar.config
{deps, [{qmux, "~> 1.12"}]}.

gleam add qmux and {:qmux, "~> 1.12"} in mix.exs work too.

Versioning: the package has a version of its own, and is released only when it changes. QMux is still an Internet-Draft, so a new draft revision may change the API and the wire format.

Use

QMux has no ALPN identifier of its own. The application defines one per draft version, such as myapp-qmux-02, and negotiates it with TLS ALPN, or as the WebSocket subprotocol, before it starts the session.

{ok, Socket} = ssl:connect("example.com", 443, [
    binary, {active, false}, {alpn_advertised_protocols, [<<"myapp-qmux-02">>]}
]),
{ok, Session, Queue} = qmux:start({qmux_ssl, Socket}, client, self(), #{
    parameters => #{
        max_idle_timeout => 30000,
        initial_max_data => 1 bsl 20,
        initial_max_stream_data_bidi_local => 256 bsl 10,
        initial_max_stream_data_uni => 256 bsl 10,
        initial_max_streams_uni => 16,
        max_datagram_frame_size => 1200,
        max_record_size => 64 bsl 10
    },
    keep_alive => 10000
}),
receive
    {qmux, Session, {connected, _}} -> ok
end,
{ok, Stream} = qmux:open_stream(Session),
ok = qmux:send_data(Session, Queue, Stream, <<"hello">>, true).

A server does the same with server on each accepted connection, and receives {qmux, Session, {stream_opened, Id}}, {stream_data, Id, Data, Fin} and {datagram, Data} events.

Not supported: 0-RTT, and the RESET_STREAM_AT extension (draft-02 §9.2). Neither is advertised, so peers do not use them.

Development

rebar3 eunit      # the shared golden vectors in ../../testdata, and sessions against a hand-written peer over TLS and TCP
rebar3 xref
rebar3 dialyzer

nix build .#trevrpc-erlang-qmux runs all three, and the trevrpc-erlang-qmux-lint check runs erlfmt. trevrpc depends on this package through _checkouts/qmux (rebar3) and a path dependency (Gleam); when this package is released with trevrpc, flake-release publishes it to Hex first with rebar3, and otherwise trevrpc requires the version it is already released at; either way the path dependency becomes a Hex requirement.