#broadcasting #message #p2p #scatter #protocols

libp2p-scatter

libp2p protocol for broadcasting messages to connected peers

5 releases (3 breaking)

Uses new Rust 2024

0.4.0-rc.1 Jan 27, 2026
0.3.0 Jul 9, 2025
0.2.0 Jan 15, 2025
0.1.1 Dec 4, 2024
0.1.0 Nov 28, 2024

#665 in Network programming

Download history 843/week @ 2025-11-19 589/week @ 2025-11-26 543/week @ 2025-12-03 578/week @ 2025-12-10 637/week @ 2025-12-17 65/week @ 2025-12-24 396/week @ 2025-12-31 536/week @ 2026-01-07 1202/week @ 2026-01-14 1285/week @ 2026-01-21 1745/week @ 2026-01-28 1287/week @ 2026-02-04 940/week @ 2026-02-11 1279/week @ 2026-02-18 2247/week @ 2026-02-25 1593/week @ 2026-03-04

6,295 downloads per month
Used in 8 crates (2 directly)

MIT/Apache

98KB
2K SLoC

API Documentation Build Status codecov Rust Stable Apache 2.0 Licensed MIT Licensed

libp2p-scatter

Implementation of a rust-libp2p protocol for broadcast messages to connected peers.

Originally forked from https://github.com/cloudpeers/libp2p-broadcast.

API Overview

// Create behaviour
let mut behaviour = Behaviour::new(Config::default());

// Subscribe to topics
behaviour.subscribe(Topic::new(b"announcements"));

// Broadcast messages
behaviour.broadcast(&topic, Bytes::from("Hello!"));

// Query subscriptions
for topic in behaviour.subscribed() {
    println!("Subscribed to: {topic}");
}

// Query peers on a topic
if let Some(peers) = behaviour.peers(&topic) {
    for peer in peers {
        println!("Peer: {peer}");
    }
}

Example

use futures::StreamExt;
use libp2p::swarm::SwarmEvent;
use libp2p::{Multiaddr, identity, noise, tcp, yamux};

use bytes::Bytes;
use libp2p_scatter as scatter;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Parse command line arguments (if any)
    // Usage: ./example [to-peer-multiaddr]
    let args: Vec<String> = std::env::args().collect();

    // If a multiaddr is provided, the node will attempt to connect to that peer.
    let connect_addr = if args.len() > 1 {
        Some(args[1].parse::<Multiaddr>()?)
    } else {
        None
    };

    // Create a new libp2p identity
    let local_key = identity::Keypair::generate_ed25519();
    let local_peer_id = local_key.public().to_peer_id();
    println!("Local peer id: {local_peer_id}");

    // Create a scatter behaviour with default config
    let behaviour = scatter::Behaviour::new(scatter::Config::default());

    // Build the swarm
    let mut swarm = libp2p::SwarmBuilder::with_existing_identity(local_key)
        .with_tokio()
        .with_tcp(
            tcp::Config::default(),
            noise::Config::new,
            yamux::Config::default,
        )?
        .with_behaviour(|_| behaviour)?
        .build();

    // Listen on a local address
    swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;

    if let Some(addr) = connect_addr {
        swarm.dial(addr.clone())?;
        println!("Dialed {addr}");
    }

    // Subscribe to a topic
    let topic = scatter::Topic::new(b"my-topic");
    swarm.behaviour_mut().subscribe(topic);

    // Event loop
    loop {
        match swarm.select_next_some().await {
            SwarmEvent::Behaviour(scatter::Event::Subscribed(peer_id, topic)) => {
                println!("Peer {peer_id} subscribed to topic: {topic}");

                // Broadcast a message when a peer subscribes
                let message = Bytes::from("Hello, peer!");
                swarm.behaviour_mut().broadcast(&topic, message);
            }
            SwarmEvent::Behaviour(scatter::Event::Unsubscribed(peer_id, topic)) => {
                println!("Peer {peer_id} unsubscribed from topic: {topic}");
            }
            SwarmEvent::Behaviour(scatter::Event::Received(peer_id, topic, message)) => {
                println!("Received message from {peer_id} on {topic}: {message:?}");
            }
            SwarmEvent::NewListenAddr { address, .. } => {
                println!("Listening on {address}");
            }
            SwarmEvent::ConnectionEstablished { peer_id, .. } => {
                println!("Connected to {peer_id}");
            }
            _ => {}
        }
    }
}

License

MIT OR Apache-2.0

Dependencies

~14–19MB
~263K SLoC