Skip to content

Overview

dart_libp2p_webrtc is the libp2p WebRTC transport for dart_libp2p. It lets a Dart or Flutter app and a web app running js-libp2p in a browser connect directly to each other. Two Dart apps can use it too.

Browsers can’t open raw TCP or UDP connections, so they can’t use the transports dart_libp2p speaks natively. They can use WebRTC, and libp2p specifies how: the /webrtc transport, also called private-to-private WebRTC. The two peers find each other through a circuit relay, exchange SDP over a stream on the relayed connection, and then talk peer to peer over WebRTC data channels. The relay only carries the signaling.

sequenceDiagram
    participant B as Browser (js-libp2p)
    participant R as Circuit relay
    participant P as App (dart_libp2p)
    P->>R: reserve (TCP or UDX)
    B->>R: reserve (WebSockets)
    B->>R: connect to P through the relay
    R->>P: relayed connection
    B->>P: /webrtc-signaling/0.0.1: SDP offer
    P->>B: SDP answer, ICE candidates
    B-->>P: direct WebRTC connection (DTLS, data channels)

Under the hood it uses libdatachannel, a C/C++ WebRTC library, through dart:ffi. The package’s build hook compiles it from source for Android, iOS, Linux and macOS, so there’s no separate native install.

Terminal window
dart pub add dart_libp2p_webrtc
Feature Notes
libp2p /webrtc Dials and accepts /p2p-circuit/webrtc connections, per the libp2p WebRTC spec. Interoperates with js-libp2p in Chrome, Firefox and WebKit.
Swarm integration WebRTC connections join the dart_libp2p swarm like TCP or UDX ones: your stream handlers, identify and ping work over them unchanged.
Streams Data channels with the spec’s framing: half-close (FIN, FIN_ACK), STOP_SENDING and resets, so they behave like any libp2p stream.
upgrade(peer) Moves a peer you reach through a relay onto a direct connection.
NAT traversal ICE with STUN, and TURN for networks with no direct path.
Data channels without libp2p RtcPeerConnection and RtcDataChannel, for WebRTC outside libp2p.

Every code sample in these guides comes from a program in example/ or from the browser page the interop tests run, and the test suite runs them.

Built by Stephan Februarygithub.com/stephanfebLinkedIn