Architecture Overview
Understanding the architecture of dart-libp2p is key to using it effectively. The library is designed to be modular, with components that can be swapped and configured to suit different needs. This document provides a high-level overview of the core components and how they interact.
The Libp2p Stack
Section titled “The Libp2p Stack”The libp2p stack can be visualized as a series of layers, where each layer provides services to the one above it. Data flows down the stack for outgoing communication and up the stack for incoming communication.
graph TD
subgraph "Your Application"
A[Application Logic / Custom Protocols]
end
subgraph "Core Libp2p Stack"
H(Host Interface)
N(Network / Swarm)
U(Upgrader)
M(Multiplexer e.g., Yamux)
S(Security e.g., Noise)
T(Transport e.g., TCP, UDX)
end
A --> H
H --> N
N --> U
U --> M
U --> S
M --> S
S --> T
T --> NW[Physical Network]
Core Components
Section titled “Core Components”-
Host: The central entry point for your application. It ties all the other components together and provides the main API for interacting with the libp2p network.
-
Network / Swarm: The brain of the stack. The
Swarmis the concrete implementation of theNetworkinterface. It manages connections, peers, and the lifecycle of the upgrade process. -
Upgrader: Responsible for upgrading raw, insecure connections into fully featured, secure, and multiplexed connections. It orchestrates the negotiation of security protocols and stream multiplexers.
-
Transport: The foundation of the stack. Transports like
TcpTransportorUdxTransportare responsible for establishing the initial raw connections over the physical network. -
Security: The security layer, with
NoiseSecurityas the default, secures the raw connection by encrypting all communication and authenticating the remote peer’s identity. -
Multiplexer: The multiplexing layer, with
YamuxSessionas the default, takes a single secure connection and allows multiple independent, concurrent streams to run over it. -
Application / Protocols: This is where your application logic lives. You define
StreamHandlerfunctions to handle incoming streams for your custom protocols.