4 releases (2 breaking)

0.3.0 Apr 23, 2021
0.2.2 Mar 2, 2021
0.2.1 Jan 26, 2021
0.1.0 Jan 14, 2021

#35 in #libp2p

Download history 8/week @ 2024-02-26 78/week @ 2024-04-01

78 downloads per month
Used in libp2p-rs

MIT license

525KB
9K SLoC

libp2prs-floodsub

the baseline flooding protocol

This is the canonical pubsub implementation for libp2p-rs.

Usage

step1: create floodsub and get handler

    let floodsub = FloodSub::new(FloodsubConfig::new(local_peer_id));
    let handler = floodsub.handler();

step2: register handler to swarm

    let swarm = Swarm::new(keys.public())
        .with_transport(Box::new(tu))
        .with_protocol(Box::new(handler))
        .with_ping(PingConfig::new().with_unsolicited(true).with_interval(Duration::from_secs(1)))
        .with_identify(IdentifyConfig::new(false));

step3: get floodsub control and then start with swarm control

    let floodsub_control = floodsub.control();
    floodsub.start(swarm.control());

step4: start swarm

    // listen on
    swarm.listen_on(vec![listen_addr]).unwrap();
    // start swarm
    swarm.start();
    // new connection
    swarm_control.new_connection(remote_peer_id).await.unwrap();

step5: publish/subscribe/ls/getPeers

subscribe

    task::spawn(async move {
        let sub = control.subscribe(b"test").await;
        if let Some(mut sub) = sub {
            loop {
                if let Some(msg) = sub.ch.next().await { log::info!("recived: {:?}", msg.data) }
            }
        }
    });

publish

    floodsub_control.publish(Topic::new(b"test"), msg).await;

ls

    floodsub_control.ls().await;

getPeers

    floodsub_control.get_peers(Topic::new(b"test"));

TODO list:

  • config item: sign strict
  • filter repetitive message to prevent over flood
  • blacklist

Dependencies

~19–34MB
~618K SLoC