A comprehensive Dart implementation of the libp2p networking stack, providing a modular and extensible foundation for building peer-to-peer applications.
- Modular Architecture: Pluggable transports, security protocols, and stream multiplexers
- Multiple Transports: TCP and custom UDX (UDP-based) transport support
- Security: Noise protocol for encrypted and authenticated connections
- Stream Multiplexing: Yamux for efficient multi-stream communication
- Peer Discovery: mDNS and routing-based peer discovery mechanisms
- Protocol Support: Built-in support for Ping, Identify, and other core libp2p protocols
- Resource Management: Built-in protection against resource exhaustion
- Event System: Comprehensive event bus for monitoring network activity
- NAT Traversal: Hole punching and relay support for NAT traversal
Add dart_libp2p
to your pubspec.yaml
:
dependencies:
dart_libp2p: ^0.5.2
Then run:
dart pub get
Here's a simple example of creating two libp2p nodes and connecting them:
import 'package:dart_libp2p/dart_libp2p.dart';
import 'package:dart_libp2p/config/config.dart' as p2p_config;
import 'package:dart_libp2p/core/crypto/ed25519.dart' as crypto_ed25519;
import 'package:dart_libp2p/core/multiaddr.dart';
import 'package:dart_libp2p/p2p/security/noise/noise_protocol.dart';
import 'package:dart_libp2p/p2p/transport/udx_transport.dart';
import 'package:dart_libp2p/p2p/transport/connection_manager.dart' as p2p_conn_manager;
import 'package:dart_udx/dart_udx.dart';
Future<Host> createHost({String? listen}) async {
final keyPair = await crypto_ed25519.generateEd25519KeyPair();
final udx = UDX();
final connMgr = p2p_conn_manager.ConnectionManager();
final options = <p2p_config.Option>[
p2p_config.Libp2p.identity(keyPair),
p2p_config.Libp2p.connManager(connMgr),
p2p_config.Libp2p.transport(UDXTransport(connManager: connMgr, udxInstance: udx)),
p2p_config.Libp2p.security(await NoiseSecurity.create(keyPair)),
if (listen != null) p2p_config.Libp2p.listenAddrs([MultiAddr(listen)]),
];
final host = await p2p_config.Libp2p.new_(options);
await host.start();
return host;
}
void main() async {
final host1 = await createHost(listen: '/ip4/0.0.0.0/udp/0/udx');
final host2 = await createHost(listen: '/ip4/0.0.0.0/udp/0/udx');
print('Host 1: ${host1.id}');
print('Host 2: ${host2.id}');
await host1.connect(AddrInfo(host2.id, host2.addrs));
print('Connected successfully!');
await host1.close();
await host2.close();
}
Dart Libp2p follows a layered architecture where each component provides services to the layer above it:
┌─────────────────────────────────────┐
│ Application │
│ (Your Custom Protocols) │
├─────────────────────────────────────┤
│ Host │
├─────────────────────────────────────┤
│ Network/Swarm │
├─────────────────────────────────────┤
│ Upgrader │
├─────────────────────────────────────┤
│ Multiplexer (Yamux) │ Security │
│ │ (Noise) │
├─────────────────────────────────────┤
│ Transport │
│ (TCP, UDX) │
└─────────────────────────────────────┘
- Host: Central entry point that ties all components together
- Network/Swarm: Manages connections, peers, and upgrade lifecycle
- Upgrader: Handles security and multiplexing negotiation
- Transport: Establishes raw connections (TCP, UDX)
- Security: Encrypts and authenticates connections (Noise)
- Multiplexer: Enables multiple streams over single connections (Yamux)
- Protocols:
/ip4/tcp
,/ip6/tcp
- Use Case: Reliable, widely available transport for most applications
- Best For: Data centers, servers with public IPs
- Protocols:
/ip4/udp/udx
,/ip6/udp/udx
- Use Case: Custom UDP-based transport with built-in reliability
- Best For: NAT traversal, hole punching, peer-to-peer connections
Note: This implementation does not support QUIC. Instead, we've opted for a custom
dart-udx
implementation that provides similar benefits for peer-to-peer networking.
Dart Libp2p uses the Noise protocol for securing connections:
- Encryption: All communication is encrypted
- Authentication: Remote peer identity is verified
- Perfect Forward Secrecy: Session keys are ephemeral
- Handshake: Efficient key exchange and authentication
For detailed documentation, visit the docs directory:
- Getting Started: Step-by-step setup guide
- Architecture: Detailed architecture overview
- Configuration: Configuration options
- Host: Host component documentation
- Transports: Transport layer details
- Security: Security protocol information
- Multiplexing: Stream multiplexing
- Protocols: Built-in protocol documentation
- Peerstore: Peer information management
- Event Bus: Event system documentation
- Resource Manager: Resource protection
- Cookbook: Practical examples and recipes
Check out the examples directory for working examples:
Example | Type | Description | Key Features |
---|---|---|---|
echo_basic | Basic | Simple echo server/client | One-way messaging, connection basics |
chat_mdns | Advanced | mDNS-enabled P2P chat | mDNS discovery, multi-peer chat, zero-config networking |
The chat_mdns
example demonstrates mDNS service discovery using the mdns_dart
package:
- ✅ Real mDNS service advertisement (not fake lookup calls)
- ✅ Actual network-level discovery (broadcasts to 224.0.0.251:5353)
- ✅ Zero-configuration networking (no fallback mechanisms needed)
- ✅ Cross-subnet support (works beyond localhost)
Run multiple instances to see real peer discovery in action:
# Terminal 1
dart run example/chat_mdns/main.dart
# Terminal 2
dart run example/chat_mdns/main.dart
Run the test suite:
dart test
We welcome contributions! Please see our contributing guidelines and code of conduct.
This project is licensed under the MIT License - see the LICENSE file for details.