trevrpc-erlang
The BEAM runtime for TrevRPC, an RPC framework like gRPC built on QUIC instead of HTTP/2: one Hex package, trevrpc, for Erlang, Gleam and Elixir projects, with servers that serve native QUIC and QMux over TLS (and, when enabled, the HTTP mapping over HTTP/3 and HTTP/2 and browser sessions over WebTransport and QMux over WebSocket), and native QUIC clients that fall back to QMux over TLS when UDP is blocked. It is pure Erlang, built on erlang_quic, erlang-webtransport, erlang_h2, erlang_h1, erlang_ws, telemetry and OTP’s ssl and zstd, with its QMux implementation in a Hex package of its own, qmux, and needs Erlang/OTP 29 or later. A typed Gleam binding is part of the package, and protoc-gen-trevrpc-elixir generates Elixir code over the runtime.
Compatibility: the TrevRPC runtimes and code generators share one version, and runtimes of the same version are compatible with each other:
trevrpc-rust1.14.0 works withtrevrpc-go1.14.0,trevrpc-js1.14.0,trevrpc-kotlin1.14.0,trevrpc-erlang1.14.0, andtrevrpc-py1.14.0. Compatibility across different versions isn’t guaranteed yet; that’s a milestone for 2.0. The QMux packages and the integrations have versions of their own.
Packages
| Package | Install | Purpose |
|---|---|---|
trevrpc | rebar.config: {trevrpc, "~> 1.12"}; gleam add trevrpc; mix.exs: {:trevrpc, "~> 1.12"} | Client and server runtime, with the Gleam binding |
qmux | rebar.config: {qmux, "~> 1.12"}; gleam add qmux; mix.exs: {:qmux, "~> 1.12"} | Standalone QMux (draft-ietf-quic-qmux-02): QUIC streams and datagrams over TLS or WebSocket |
trevrpc_opentelemetry | rebar.config: {trevrpc_opentelemetry, "~> 0.1"}; gleam add trevrpc_opentelemetry; mix.exs: {:trevrpc_opentelemetry, "~> 0.1"} | OpenTelemetry server spans, metrics and propagation |
autocert | rebar.config: {autocert, "~> 0.1"}; gleam add autocert; mix.exs: {:autocert, "~> 0.1"} | ACME certificates through TLS-ALPN-01, for any TLS server |
trevrpc_protovalidate | rebar.config: {trevrpc_protovalidate, "~> 0.1"}; gleam add trevrpc_protovalidate; mix.exs: {:trevrpc_protovalidate, "~> 0.1"} | protovalidate request validation with CEL rules for servers and channels |
protoc-gen-trevrpc-erlang | escript: the Nix package below, or rebar3 escriptize in protoc-gen-trevrpc-erlang/ | protoc plugin: gpb message modules and service behaviours, routes and clients |
protoc-gen-trevrpc-gleam | escript: the Nix package below, or rebar3 escriptize in protoc-gen-trevrpc-gleam/ | protoc plugin: Gleam types and a module per service |
protoc-gen-trevrpc-elixir | escript: the Nix package below, or rebar3 escriptize in protoc-gen-trevrpc-elixir/ | protoc plugin: an Elixir module per service |
The trevrpc-erlang Nix package provides all three plugins: nix shell git+https://trev.zip/llc/TrevRPC.git#trevrpc-erlang.
Example
Generate code for a service with protoc-gen-trevrpc-erlang:
protoc --plugin=protoc-gen-trevrpc-erlang=path/to/protoc-gen-trevrpc-erlang \
-I proto -I path/to/TrevRPC/proto --trevrpc-erlang_out=src/generated example/greeter/v1/greeter.proto
Serve it:
say_hello(#{name := Name}, _Call) ->
{ok, #{message => <<"hello ", Name/binary>>}}.
{ok, Server} = trevrpc_server:start_link(#{
port => 4433,
certfile => "cert.pem",
keyfile => "key.pem",
routes => example_greeter_v1_greeter:routes(#{say_hello => fun say_hello/2})
}).
And call it:
{ok, Channel} = trevrpc_channel:connect(#{host => "localhost", port => 4433, cacertfile => "ca.pem"}, 5000),
{ok, #{message := Message}, _} = example_greeter_v1_greeter:say_hello(Channel, #{name => <<"you">>}).
The guide has the same service in Gleam and Elixir.
Documentation
The Erlang guide covers generated code, servers, clients, compression, statuses, request validation, the Gleam binding and its code generation, and Elixir code generation. The rest of the wiki covers the transports, the protocol, compression, and operations.
Development
rebar3 and Erlang/OTP 29 are in the repository’s dev shell; the Gleam binding also needs gleam (nix shell nixpkgs#gleam) and protoc.
rebar3 eunit # unit tests, the shared golden vectors in ../testdata, client/server over QUIC and QMux, datagram messages, QMux over WebSocket, and the HTTP mapping
rebar3 xref
rebar3 dialyzer
nix build .#trevrpc-erlang runs all three. nix fmt formats the sources with erlfmt, and the Gleam sources with gleam format; the trevrpc-erlang-lint check runs erlfmt. The same commands work in protoc-gen-trevrpc-erlang/, protoc-gen-trevrpc-gleam/ and protoc-gen-trevrpc-elixir/, whose tests also need protoc, and nix build .#trevrpc-erlang runs them too, as it builds the plugins it provides.
./gleam-test.sh # gleam-generate.sh, which generates the Gleam tests' service with protoc-gen-trevrpc-gleam, then gleam test
The Gleam tests (test/trevrpc_test.gleam) run a Gleam server and client on loopback. gleam test builds the plugin in protoc-gen-trevrpc-gleam/ unless PROTOC_GEN_TREVRPC_GLEAM names it, and runs only that module, as rebar3 runs the Erlang tests. The trevrpc-erlang-gleam check (nix build .#trevrpc-erlang.gleam, from gleam.nix) builds the package as gleam publish does and runs them offline.
protoc-gen-trevrpc-gleam and protoc-gen-trevrpc-elixir build on protoc-gen-trevrpc-erlang: each carries that plugin’s modules, which make gpb’s modules, check methods and read their options, and takes them from _checkouts, a link to it, so changes there reach them too. The Gleam plugin also carries the plugin protocol (trevrpc_gen_plugin), and its tests share test/support and test/proto.
protoc-gen-trevrpc-gleam/test/gleamis a Gleam project that runs the generated code:test/gleam/test.shgenerates it (generate.sh), runs its tests, and checks with protoc that its bytes decode as expected (compare.sh). Thetrevrpc-erlang-protoc-gen-gleamcheck runs it.protoc-gen-trevrpc-elixir/test/elixiris a Mix project that runs the generated code:test/elixir/test.shgenerates it intogen/and runs an Elixir server and client through it withmix test, takingtrevrpcfromERL_LIBSrather than Hex, with Elixir on the PATH. Thetrevrpc-erlang-protoc-gen-elixircheck runs it.
qmux/ is the qmux package, with its own tests (see its README). rebar3 takes it from _checkouts/qmux, a link to it, both here and in interop/, and Gleam from the path dependency in gleam.toml, which flake-release replaces with the version it publishes to Hex just before trevrpc.
opentelemetry/ is the trevrpc_opentelemetry package, which Gleam alone builds and tests, with trevrpc and qmux from its path dependencies (see its README). flake-release publishes it with gleam publish after trevrpc, which its path dependency becomes a Hex requirement on.
autocert/ is the autocert package, which depends on no TrevRPC package and which Gleam alone builds and tests, its tests getting a certificate from Pebble (see its README). flake-release publishes it with gleam publish.
protovalidate/ is the trevrpc_protovalidate package, which, like trevrpc, rebar3 and Gleam both build and test, with trevrpc and qmux from _checkouts links and from its path dependency (see its README), and the interop executable takes from _checkouts/trevrpc_protovalidate. flake-release publishes it with gleam publish after trevrpc, which its path dependency becomes a Hex requirement on.
The interop executable, trevrpc-interop-erlang (the trevrpc-erlang-interop package), is in the interop suite and has the load benchmark’s client. Its bindings, in interop/src/generated, are regenerated with interop/generate.sh, and a check fails when they differ from what the plugin generates.
erlang_quic builds its optional crypto NIF only when QUIC_BUILD_NIF=1 is set while the quic dependency compiles, which needs CMake, a C compiler and the OpenSSL headers; the Nix packages set it. Neither rebar3 nor Gleam rebuilds a dependency it already compiled, so remove it first (rm -rf _build/default/lib/quic && QUIC_BUILD_NIF=1 rebar3 compile, or rm -rf build/dev/erlang/quic && QUIC_BUILD_NIF=1 gleam build). quic_crypto_nif:is_loaded() says whether it loaded; see the guide’s crypto NIF section.
rebar3 builds the application from rebar.config and src/trevrpc.app.src, Gleam from gleam.toml, which gleam publish turns into the Hex package’s metadata and .app file. The Hex dependencies are declared in gleam.toml and locked in manifest.toml, which Renovate’s Gleam manager updates; no Renovate manager reads rebar3’s files. nix run .#sync-erlang-deps (erlang_deps.py) copies them into rebar.config (the ranges, verbatim, as rebar3 reads Hex’s syntax), rebar.lock (the locked versions and their checksums, the inner ones from the packages’ tarballs), protovalidate/‘s rebar.config and rebar.lock from its own gleam.toml and manifest.toml, the interop executable’s rebar.lock, which locks both packages’ dependencies, and protoc-gen-trevrpc-gleam’s and protoc-gen-trevrpc-elixir’s gpb, which follows protoc-gen-trevrpc-erlang’s. Renovate runs it after updating the Gleam files; after changing a dependency by hand, change gleam.toml, run gleam update (or gleam deps download), then the sync. The Nix package reads versions and tarball hashes from rebar.lock, and the trevrpc-erlang-deps check fails when rebar3’s files drift from the Gleam ones. python3 -I -m unittest discover -s trevrpc-erlang, from the repository’s root, runs the sync’s tests, as the python-test check does. gleam_stdlib is the one dependency only gleam.toml has, qmux, a path dependency there, is required by name in rebar.config, and adding or removing one takes both gleam.toml and rebar.config.
See Local Development for the repository-wide checks.