Protocols
At this point, we’re connected, yay! Now we just have to… do something… with that connection. That’s where protocols come in.
Protocols are modules you compose together to add functionality to connections. Protocols exist for file transfer, game server sync, message broadcasting, document collaboration, all kinds of stuff. You can use off-the-shelf protocols to quickly add functionality, work directly with QUIC streams & build a fully custom protocol that does exactly what you want, or fork an existing protocol to get what you need.
Coming from the world of HTTP client/server models, protocols are kinda like request handlers, but they can go beyond request/response creating multiple streams of data and usually code up both initiating & accepting protocol connections in the same.
Protocols are an ever-growing topic, but to give you a basic let's add the blobs protocol. First we need to add it to our depdendencies:
cargo add iroh-blobs
then adjust our code:
use iroh::{protocol::Router, Endpoint};
use iroh_blobs::{net_protocol::Blobs, util::local_pool::LocalPool};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
let local_pool = LocalPool::default();
let blobs = Blobs::memory().build(local_pool.handle(), &endpoint);
// build the router
let router = Router::builder(endpoint)
.accept(iroh_blobs::ALPN, blobs.clone())
.spawn()
.await?;
router.shutdown().await?;
drop(local_pool);
drop(tags_client);
Ok(())
}
This code doesn't actually do anything with the blobs protocol. For a real-world example, check out sendme
This code sets up everything we need to both provide data we have locally when others request it, and ask other nodes that run the blobs protocol for data. Starting at the top, we first construct the endpoint, then we construct an instance of the blobs protocol, then add a router (more on that in a minute) that listens for blobs protocol connections.