use iroh::{protocol::Router, Endpoint, EndpointId};
use iroh_gossip::{api::Event, Gossip, TopicId};
use n0_error::{Result, StdResultExt};
use n0_future::StreamExt;
#[tokio::main]
async fn main() -> Result<()> {
// create an iroh endpoint that includes the standard discovery mechanisms
// we've built at number0
let endpoint = Endpoint::bind().await?;
// build gossip protocol
let gossip = Gossip::builder().spawn(endpoint.clone());
// setup router
let router = Router::builder(endpoint)
.accept(iroh_gossip::ALPN, gossip.clone())
.spawn();
// gossip swarms are centered around a shared "topic id", which is a 32 byte identifier
let topic_id = TopicId::from_bytes([23u8; 32]);
// and you need some bootstrap peers to join the swarm
let bootstrap_peers = bootstrap_peers();
// then, you can subscribe to the topic and join your initial peers
let (sender, mut receiver) = gossip
.subscribe(topic_id, bootstrap_peers)
.await?
.split();
// you might want to wait until you joined at least one other peer:
receiver.joined().await?;
// then, you can broadcast messages to all other peers!
sender.broadcast(b"hello world this is a gossip message".to_vec().into()).await?;
// and read messages from others
while let Some(event) = receiver.next().await {
match event? {
Event::Received(message) => {
println!("received a message: {:?}", std::str::from_utf8(&message.content));
}
_ => {}
}
}
// clean shutdown makes sure that other peers are notified that you went offline
router.shutdown().await.std_context("shutdown router")?;
Ok(())
}
fn bootstrap_peers() -> Vec<EndpointId> {
// insert your bootstrap peers here, or get them from your environment
vec![]
}