58 releases (34 breaking)

0.45.1 Aug 9, 2024
0.44.2 May 1, 2024
0.44.1 Dec 9, 2023
0.44.0 Nov 5, 2023
0.1.0 Jul 19, 2019

#1942 in Asynchronous

Download history 69656/week @ 2024-08-06 80572/week @ 2024-08-13 66800/week @ 2024-08-20 75338/week @ 2024-08-27 84678/week @ 2024-09-03 70239/week @ 2024-09-10 73592/week @ 2024-09-17 74850/week @ 2024-09-24 79276/week @ 2024-10-01 71964/week @ 2024-10-08 77513/week @ 2024-10-15 75500/week @ 2024-10-22 66317/week @ 2024-10-29 78064/week @ 2024-11-05 64553/week @ 2024-11-12 56323/week @ 2024-11-19

279,925 downloads per month
Used in 358 crates (29 directly)

MIT license

1MB
16K SLoC

High-level network manager.

A Swarm contains the state of the network as a whole. The entire behaviour of a libp2p network can be controlled through the Swarm. The Swarm struct contains all active and pending connections to remotes and manages the state of all the substreams that have been opened, and all the upgrades that were built upon these substreams.

Initializing a Swarm

Creating a Swarm requires three things:

  1. A network identity of the local node in form of a PeerId.
  2. An implementation of the Transport trait. This is the type that will be used in order to reach nodes on the network based on their address. See the transport module for more information.
  3. An implementation of the NetworkBehaviour trait. This is a state machine that defines how the swarm should behave once it is connected to a node.

Network Behaviour

The NetworkBehaviour trait is implemented on types that indicate to the swarm how it should behave. This includes which protocols are supported and which nodes to try to connect to. It is the NetworkBehaviour that controls what happens on the network. Multiple types that implement NetworkBehaviour can be composed into a single behaviour.

Protocols Handler

The ConnectionHandler trait defines how each active connection to a remote should behave: how to handle incoming substreams, which protocols are supported, when to open a new outbound substream, etc.

Dependencies

~8–21MB
~304K SLoC