3. Discovery
Discovery is the glue that connects a Endpoint Identifier to something we can dial. There are a few different types of discovery services, but for all of them you put a EndpointID
in, and get back either the home relay of that endpoint, or IP addresses to dial.
There are different implementations of the discovery service in iroh, the most popular of which are DNS & Local Discovery. DNS uses the same domain name system that connects "example.com" to an IP address to map endpoint ids to relay servers, and local discovery uses your local network to find endpoints to talk to on local WiFi, even if that WiFi network doesn’t have a wider internet connection.
DNS Discovery
Iroh endpoints come with some defaults that include using our public infrastructure to enable discovery:
use iroh::{Endpoint, RelayMode};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let builder = Endpoint::.bind().await?;
println!("endpoint id: {:?}", endpoint.id());
Ok(())
}
Now when this endpoint boots up, it will list itself on the n0 DNS service. From here we can pass along the endpoint identifier, and other endpoints can use the n0 DNS service to find the home relay of this endpoint.
Local Discovery
Local discovery has the extra trick of being able to actually find new endpoints on the local network. Before we can do that, we need to add the discovery-local-network
feature to our Cargo.toml
file:
cargo add iroh --features discovery-local-network
This will change our Cargo.toml
file [dependencies]
section to look like this:
[dependencies]
anyhow = "1.0.95"
iroh = { version = "0.32.1", features = ["discovery-local-network"] }
rand = "0.8.5"
tokio = "1.43.0"
And with that we can set up local discovery, alongside our default discovery:
use iroh::{Endpoint, RelayMode, SecretKey};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mdns = iroh::discovery::mdns::MdnsDiscovery::builder();
let builder = Endpoint::builder()
.discovery(mdns);
let endpoint = builder.bind().await?;
println!("endpoint id: {:?}", endpoint.id());
Ok(())
}
Here we’ve added discovery to the endpoint constructor, passing in our two default and one custom discovery services, and that’s it, iroh will now use these three services to get something it can dial for a given endpoint ID.
For an example of this in action, check out the local discovery example.