Top Related Projects
libp2p implementation in Go
The JavaScript Implementation of libp2p networking stack.
Matrix Client-Server SDK for Rust
Quick Overview
rust-libp2p is a modular peer-to-peer networking stack implemented in Rust. It provides a set of protocols and tools for building decentralized applications and networks, offering a flexible and efficient framework for peer-to-peer communication.
Pros
- Written in Rust, providing memory safety and performance benefits
- Modular design allows for easy customization and extension
- Supports multiple transport protocols and encryption methods
- Active development and community support
Cons
- Learning curve for developers new to Rust or peer-to-peer networking
- Documentation can be sparse or outdated in some areas
- Some features may still be in experimental stages
- Potential compatibility issues with other libp2p implementations
Code Examples
- Creating a basic libp2p node:
use libp2p::{identity, PeerId, Swarm};
use libp2p::ping::{Ping, PingConfig};
use libp2p::swarm::SwarmEvent;
#[async_std::main]
async fn main() -> Result<(), Box<dyn Error>> {
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());
let transport = libp2p::development_transport(local_key).await?;
let behavior = Ping::new(PingConfig::new().with_keep_alive(true));
let mut swarm = Swarm::new(transport, behavior, local_peer_id);
// Listen on all interfaces and a random OS-assigned port
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
loop {
match swarm.select_next_some().await {
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {:?}", address),
_ => {}
}
}
}
- Implementing a custom protocol:
use libp2p::core::ProtocolName;
use libp2p::swarm::NetworkBehaviour;
#[derive(Clone)]
struct MyProtocol;
impl ProtocolName for MyProtocol {
fn protocol_name(&self) -> &[u8] {
"/my-protocol/1.0.0".as_bytes()
}
}
#[derive(NetworkBehaviour)]
struct MyBehaviour {
my_protocol: MyProtocol,
}
- Connecting to a peer:
use libp2p::Multiaddr;
let peer_id = "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ";
let addr = "/ip4/104.131.131.82/tcp/4001".parse::<Multiaddr>()?;
swarm.dial(addr.clone())?;
println!("Dialed {}", peer_id);
Getting Started
To use rust-libp2p in your project, add the following to your Cargo.toml
:
[dependencies]
libp2p = "0.51.3"
Then, in your Rust file:
use libp2p::{identity, PeerId, Swarm};
use libp2p::ping::{Ping, PingConfig};
// Create a random PeerId
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());
// Create a transport
let transport = libp2p::development_transport(local_key).await?;
// Create a ping network behaviour
let behaviour = Ping::new(PingConfig::new().with_keep_alive(true));
// Create a Swarm to manage peers and events
let swarm = Swarm::new(transport, behaviour, local_peer_id);
Competitor Comparisons
libp2p implementation in Go
Pros of go-libp2p
- More mature and stable implementation with a longer history
- Larger community and ecosystem support
- Better integration with existing Go-based projects
Cons of go-libp2p
- Generally slower performance compared to Rust implementation
- Higher memory usage
- Less type safety and potential for runtime errors
Code Comparison
go-libp2p:
host, err := libp2p.New(
libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"),
libp2p.Identity(priv),
)
rust-libp2p:
let transport = libp2p::development_transport(local_key).await?;
let behaviour = MyBehaviour::default();
let mut swarm = Swarm::new(transport, behaviour, local_peer_id);
The Go implementation tends to use a more configuration-based approach, while the Rust version often employs a more modular and composable style. The Rust implementation generally provides stronger compile-time guarantees and more explicit error handling.
Both libraries offer similar core functionality, including peer discovery, routing, and various transport protocols. The choice between them often depends on the specific project requirements, existing technology stack, and developer preferences.
The JavaScript Implementation of libp2p networking stack.
Pros of js-libp2p
- Easier integration with web applications and Node.js projects
- Larger ecosystem of JavaScript developers and libraries
- Potentially faster development cycles due to JavaScript's dynamic nature
Cons of js-libp2p
- Generally slower performance compared to Rust implementation
- Less memory-efficient, especially for large-scale applications
- Lack of static typing may lead to more runtime errors
Code Comparison
rust-libp2p:
use libp2p::{
identity,
PeerId,
Swarm,
NetworkBehaviour,
};
let id_keys = identity::Keypair::generate_ed25519();
let peer_id = PeerId::from(id_keys.public());
js-libp2p:
import { createLibp2p } from 'libp2p'
import { noise } from '@chainsafe/libp2p-noise'
import { mplex } from '@libp2p/mplex'
const node = await createLibp2p({
transports: [/* ... */],
connectionEncryption: [noise()],
streamMuxers: [mplex()]
})
Both implementations offer similar core functionality, but with syntax and patterns typical of their respective languages. The Rust version provides more explicit type declarations and memory management, while the JavaScript version offers a more concise and flexible approach to configuration.
Matrix Client-Server SDK for Rust
Pros of matrix-rust-sdk
- Focused on Matrix protocol, providing a more specialized and comprehensive solution for Matrix-based applications
- Includes high-level abstractions for common Matrix operations, simplifying development
- Offers built-in support for end-to-end encryption, a crucial feature for secure messaging
Cons of matrix-rust-sdk
- Limited to Matrix ecosystem, less versatile for general peer-to-peer networking
- Smaller community and fewer contributors compared to rust-libp2p
- Less mature project with potentially fewer features and optimizations
Code Comparison
matrix-rust-sdk:
let client = Client::new(homeserver_url)?;
let response = client.login_username(username, password).await?;
let room = client.get_joined_room(room_id)?;
room.send(RoomMessageEventContent::text_plain("Hello, Matrix!"), None).await?;
rust-libp2p:
let transport = libp2p::development_transport(keypair.clone()).await?;
let behaviour = MyBehaviour::new();
let mut swarm = Swarm::new(transport, behaviour, local_peer_id);
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
The code snippets demonstrate the different focus of each library. matrix-rust-sdk provides high-level abstractions for Matrix operations, while rust-libp2p offers lower-level networking primitives for building custom peer-to-peer applications.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Central repository for work on libp2p
This repository is the central place for Rust development of the libp2p spec.
Getting started
-
Main documentation can be found on https://docs.rs/libp2p.
-
The examples folder contains small binaries showcasing the many protocols in this repository.
-
For security related issues please file a private security vulnerability report . Please do not file a public issue on GitHub.
-
To report bugs, suggest improvements or request new features please open a GitHub issue on this repository.
-
For rust-libp2p specific questions please use the GitHub Discussions forum https://github.com/libp2p/rust-libp2p/discussions.
-
For discussions and questions related to multiple libp2p implementations please use the libp2p Discourse forum https://discuss.libp2p.io.
-
For synchronous discussions join the open rust-libp2p maintainer calls or the biweekly libp2p community calls.
Repository Structure
The main components of this repository are structured as follows:
-
core/
: The implementation oflibp2p-core
with itsTransport
andStreamMuxer
API on which almost all other crates depend. -
transports/
: Implementations of transport protocols (e.g. TCP) and protocol upgrades (e.g. for authenticated encryption, compression, ...) based on thelibp2p-core
Transport
API. -
muxers/
: Implementations of theStreamMuxer
interface oflibp2p-core
, e.g. (sub)stream multiplexing protocols on top of (typically TCP) connections. Multiplexing protocols are (mandatory)Transport
upgrades. -
swarm/
: The implementation oflibp2p-swarm
building onlibp2p-core
with the central interfacesNetworkBehaviour
andConnectionHandler
used to implement application protocols (seeprotocols/
). -
protocols/
: Implementations of application protocols based on thelibp2p-swarm
APIs. -
misc/
: Utility libraries. -
libp2p/examples/
: Worked examples of built-in application protocols (seeprotocols/
) with commonTransport
configurations.
Community Guidelines
The libp2p project operates under the IPFS Code of Conduct.
tl;dr
- Be respectful.
- We're here to help: abuse@ipfs.io
- Abusive behavior is never tolerated.
- Violations of this code may result in swift and permanent expulsion from the IPFS [and libp2p] community.
- "Too long, didn't read" is not a valid excuse for not knowing what is in this document.
Maintainers
(In alphabetical order.)
- Guillaume Michel (@guillaumemichel)
- João Oliveira (@jxs)
Notable users
(open a pull request if you want your project to be added here)
- COMIT - BitcoinâMonero Cross-chain Atomic Swap.
- Forest - An implementation of Filecoin written in Rust.
- fuel-core - A Rust implementation of the Fuel protocol.
- HotShot - Decentralized sequencer in Rust developed by Espresso Systems.
- ipfs-embed - A small embeddable ipfs implementation used and maintained by Actyx.
- Homestar - An InterPlanetary Virtual Machine (IPVM) implementation used and maintained by Fission.
- beetle - Next-generation implementation of IPFS for Cloud & Mobile platforms.
- Lighthouse - Ethereum consensus client in Rust.
- Locutus - Global, observable, decentralized key-value store.
- OpenMina - In-browser Mina Rust implementation.
- rust-ipfs - IPFS implementation in Rust.
- Safe Network - Safe Network implementation in Rust.
- Starcoin - A smart contract blockchain network that scales by layering.
- Subspace - Subspace Network reference implementation
- Substrate - Framework for blockchain innovation, used by Polkadot.
- Taple - Sustainable DLT for asset and process traceability by OpenCanarias.
- Ceylon - A Multi-Agent System (MAS) Development Framework.
Top Related Projects
libp2p implementation in Go
The JavaScript Implementation of libp2p networking stack.
Matrix Client-Server SDK for Rust
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot