[Noob] Swarm vs Network

Noob warning. I am trying to understand the difference between a libp2p::core::Network and a libp2p::Swarm. What exactly is the difference? Why are they separate objects? What is their purpose in an application? Also, if I have a swarm instantiated, and I want to use the methods provided in the core::Network struct, is there a way to get a core::Network given a Swarm?

Hi @xoreo, welcome to libp2p!

I am trying to understand the difference between a libp2p::core::Network and a libp2p::Swarm . What exactly is the difference?

Our Ping Tutorial should give you a good overview, differentiating Transport, NetworkBehaviour and Swarm. Citing from the tutorial:

Transport

Next up we need to construct a transport. After all, we want to send some
bytes from A to B. A transport in libp2p provides connection-oriented
communication channels (e.g. TCP) as well as upgrades on top of those like
authentication and encryption protocols. Technically, a libp2p transport is
anything that implements the [Transport] trait.

Network behaviour

Now it is time to look at another core trait of rust-libp2p - the
[NetworkBehaviour]. While the previously introduced trait [Transport]
defines how to send bytes on the network, a [NetworkBehaviour] defines
what bytes to send on the network.

Swarm

Now that we have a [Transport] and a [NetworkBehaviour], we need
something that connects the two, allowing both to make progress. This job is
carried out by a [Swarm]. Put simply, a [Swarm] drives both a
[Transport] and a [NetworkBehaviour] forward, passing commands from the
[NetworkBehaviour] to the [Transport] as well as events from the
[Transport] to the [NetworkBehaviour].

Also, if I have a swarm instantiated, and I want to use the methods provided in the core::Network struct, is there a way to get a core::Network given a Swarm?

Can you expand on which methods on core::Network you need? Swarm should cover all of them, and if not, I would rather extend Swarm instead of exposing all of core::Network.

Let me know if the above helps :wink:

Thanks. This was helpful. However, I don’t see that Swarm inherits Network methods. I am trying to poll a list of all the connected peers (just like the Network::connected_peers() method would do). What would the best way to do this be? I have also been recommended to listen on SwarmEvents. How exactly would I "extend Swarm instead of exposing all of core::Network. Is doing SwarmEvent handling the way to go? Because there are events for connected and disconnected peers (SwarmEvent::ConnectionEstablished and SwarmEvent::ConnectionClosed).

Yes, libp2p-swarm follows an event-driven architecture. Thus, instead of exposing methods of core::Network in a synchronous manner, it emits events like SwarmEvent::ConnectionEstablished.

Does that help?

Is there any example code for handling SwarmEvents? Or can I assume that it is done in the same way as other network events. For example, Mdns events in This example here. ?

The MDNS example is handling swarm events here:

This makes sense, thanks for the great replies.

Great. Let us know what you end up building @xoreo! :slight_smile: