UDX: Go, Dart, JS
This page shows how to build a libp2p host that reaches a go-ricochet server over UDX in Go, Dart and JavaScript. UDX is the server’s default transport, and the only one every server runs unless its operator removed it, so a UDX host reaches any server with the default setup. If the server also runs TCP, QUIC, WebSocket or WebTransport, a stack with one of those can use it instead; see Other clients.
The address
Section titled “The address”A server’s address is a UDX multiaddr followed by its peer ID:
/ip4/203.0.113.10/udp/55223/udx/p2p/12D3KooW...| Part | Meaning |
|---|---|
/ip4/203.0.113.10 or /ip6/... |
Server IP; the UDX transports dial IP addresses, not DNS names |
/udp/55223 |
UDP port; 55223 unless the operator changed server.port |
/udx |
UDX on top of UDP (multiaddr protocol code 0x0300) |
/p2p/12D3KooW... |
Server peer ID, printed at startup; see First run & your peer ID |
What sits on top of UDX
Section titled “What sits on top of UDX”Each libp2p connection is one UDX connection. Its first UDX stream is upgraded like a TCP socket: multistream-select, then Noise, then Yamux. All three implementations below do this, so a host in any of them needs:
| Layer | Value |
|---|---|
| Transport | UDX, wire version 3 |
| Security | Noise (/noise) |
| Muxer | Yamux (/yamux/1.0.0) |
UDX wire version 3 does not interoperate with version 2: a mismatched peer looks unreachable. go-udx v0.1.3, dart_udx 3.0 and later, and js-udx all speak version 3.
Use go-libp2p-udx-transport v0.1.8, the version go-ricochet 1.4.1 itself uses. This host is the one in go-ricochet’s examples/mailbox, and the one ricochet-bench builds for a UDX address.
go get github.com/stephanfeb/go-libp2p-udx-transport@v0.1.8// Sketch: a UDX-only host, then a connection to the server.h, err := libp2p.New( libp2p.Identity(priv), libp2p.NoTransports, libp2p.Transport(udxtransport.NewTransport), libp2p.ListenAddrStrings("/ip4/0.0.0.0/udp/0/udx"), libp2p.Security(noise.ID, noise.New), libp2p.Muxer("/yamux/1.0.0", yamux.DefaultTransport),)if err != nil { return err}
info, err := peer.AddrInfoFromString("/ip4/203.0.113.10/udp/55223/udx/p2p/12D3KooW...")if err != nil { return err}if err := h.Connect(ctx, *info); err != nil { return err}Imports: github.com/libp2p/go-libp2p, .../core/peer, .../p2p/muxer/yamux, .../p2p/security/noise and udxtransport "github.com/stephanfeb/go-libp2p-udx-transport". Pass h to client.New; see Go: your own libp2p host to add UDX to a host that keeps other transports. The UDX protocol itself is documented at cryptopeer.net/udx/go.
dart_libp2p ships a UDXTransport built on dart_udx. Use dart_libp2p 3.0.0 or later, whose dart_udx speaks wire version 3; the current release is 4.7.1, on dart_udx 4.1. The host below is the one the ricochet package’s interop harness (bin/store_interop_test.dart) runs against go-ricochet.
// Sketch, after bin/store_interop_test.dart in ricochet-dart-client 0.2.4.Future<BasicHost> createHost(KeyPair keyPair) async { final connMgr = ConnectionManager(idleTimeout: const Duration(seconds: 60)); final udxTransport = UDXTransport(connManager: connMgr);
final yamuxConfig = MultiplexerConfig( keepAliveInterval: const Duration(seconds: 60), maxStreamWindowSize: 1024 * 1024, initialStreamWindowSize: 256 * 1024, streamWriteTimeout: const Duration(seconds: 30), maxStreams: 256, );
final options = <p2p_config.Option>[ await p2p_config.Libp2p.identity(keyPair), await p2p_config.Libp2p.connManager(connMgr), await p2p_config.Libp2p.transport(udxTransport), await p2p_config.Libp2p.security(await NoiseSecurity.create(keyPair)), await p2p_config.Libp2p.muxer('/yamux/1.0.0', (Conn secureConn, bool isClient) { return YamuxSession(secureConn as TransportConn, yamuxConfig, isClient, null); }), await p2p_config.Libp2p.listenAddrs([MultiAddr('/ip4/0.0.0.0/udp/0/udx')]), ]; return await p2p_config.Libp2p.new_(options) as BasicHost;}
// Connect to the server.final host = await createHost(await crypto_ed25519.generateEd25519KeyPair());await host.start();final target = MultiAddr('/ip4/203.0.113.10/udp/55223/udx/p2p/12D3KooW...');final serverId = PeerId.fromString(target.valueForProtocol(Protocols.p2p.name)!);await host.connect( AddrInfo(serverId, [target.decapsulate(Protocols.p2p.name)!]), context: core_context.Context(),);The imports are listed at the top of the harness file. Hand host to SFClient; see Dart client.
JavaScript
Section titled “JavaScript”js-udx publishes @go-ricochet/libp2p-udx on npm, a js-libp2p v3 transport for UDX. This section describes version 0.1.1. It needs Node.js 22 or later.
npm install @go-ricochet/libp2p-udx libp2p @chainsafe/libp2p-noise @chainsafe/libp2p-yamux// From the @go-ricochet/libp2p-udx README: a js-libp2p node that speaks UDX, Noise and Yamux.import { createLibp2p } from 'libp2p'import { noise } from '@chainsafe/libp2p-noise'import { yamux } from '@chainsafe/libp2p-yamux'import { udx } from '@go-ricochet/libp2p-udx'
const node = await createLibp2p({ addresses: { listen: ['/ip4/0.0.0.0/udp/0/udx'] }, transports: [udx()], connectionEncrypters: [noise()], streamMuxers: [yamux()]})udx() takes optional UdxTransportInit settings, such as keepAliveInterval (default 10 000 ms: a ping after that long without hearing from the peer, so idle connections outlive UDX’s 30 s idle timeout) and closeTimeout (default 5000 ms). The package also exports the UdxTransport class, the UDX multiaddr matcher and UDX_WIRE_VERSION, which is 3. Dials share one UDP socket per address family.
The transport does not run in browsers. A browser app imports @go-ricochet/libp2p-udx/multiaddr instead, which only registers the udx multiaddr codec, so js-libp2p can read the /udx addresses a server advertises.
The package’s README says it interoperates with go-libp2p-udx-transport and dart-libp2p. To talk to a server, use the JavaScript client: its Node.js entry builds a node with this transport for you. To write the protocols yourself, see Writing your own client.