Skip to content

Commit c6d9dcc

Browse files
committed
add local discovery section and quickstart example
1 parent 3eb48b0 commit c6d9dcc

File tree

4 files changed

+80
-10
lines changed

4 files changed

+80
-10
lines changed

concepts/discovery.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,13 @@ iroh natively, see below for our plans).
131131
DNS servers that support this spec will receive these Pkarr signed packets,
132132
check their signature and format validity, and then serve each contained record,
133133
with the DNS server's configured *origin domain* appended to all record names.
134+
135+
136+
## Local Discovery
137+
138+
Global servers aren't the only way to find other iroh endpoint. Iroh also supports local
139+
discovery, where endpoints on the same local network can find each other &
140+
exchange dialing information without a relay using mDNS. This is useful for
141+
offline environments, or for bootstrapping a network before a relay is available. For
142+
more info on configuring local discovery, see the [local discovery
143+
documentation](/connecting/local-discovery).

concepts/relays.mdx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,3 @@ rate-limit traffic that flows through the relay. This is to prevent abuse, and
3737
ensure the relays are available to everyone. If you need more capacity, you can
3838
run your own relay server, or [contact us about a custom relay
3939
setup](https://n0.computer/n0ps/).
40-
41-
## Local Discovery
42-
43-
Relays aren't the only way to find other iroh endpoint. Iroh also supports local
44-
[discovery](/concepts/discovery), where endpoints on the same local network
45-
can find each other & exchange dialing information without a relay using mDNS.
46-
This is useful for local networks, or for bootstrapping a network before a relay
47-
is available. For more info on configuring local discovery, see the [local
48-
discovery docs](https://docs.rs/iroh/latest/iroh/discovery/mdns/index.html).

connecting/local-discovery.mdx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ let ep = Endpoint::builder()
3232
.await?;
3333
```
3434

35+
The mDNS discovery mechanism will automatically broadcast your endpoint's
36+
presence on the local network, and listen for other endpoints doing the same. When
37+
another endpoint is discovered, the dialing information is exchanged, and a
38+
connection can be established directly over the local network without needing a relay.
39+
40+
For more information on how mDNS discovery works, see the [mDNS documentation](https://docs.rs/iroh/latest/iroh/discovery/mdns/index.html).
3541

3642
## Bluetooth
3743

3844
Bluetooth discovery is currently under development and will be available in a
39-
future release of iroh. For more information, please [contact us](https://cal.com/team/number-0/n0-protocol-services).
45+
future release of iroh. For more information, please [contact us](https://cal.com/team/number-0/n0-protocol-services).

quickstart.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use anyhow::{anyhow, Result};
2+
use iroh::{Endpoint, protocol::Router};
3+
use iroh_ping::Ping;
4+
use iroh_tickets::{Ticket, endpoint::EndpointTicket};
5+
use std::env;
6+
7+
async fn run_receiver() -> Result<()> {
8+
// Create an endpoint, it allows creating and accepting
9+
// connections in the iroh p2p world
10+
let endpoint = Endpoint::builder().bind().await?;
11+
12+
// bring the endpoint online before accepting connections
13+
endpoint.online().await;
14+
15+
// Then we initialize a struct that can accept ping requests over iroh connections
16+
let ping = Ping::new();
17+
18+
// get the address of this endpoint to share with the sender
19+
let ticket = EndpointTicket::new(endpoint.addr());
20+
println!("{ticket}");
21+
22+
// receiving ping requests
23+
Router::builder(endpoint)
24+
.accept(iroh_ping::ALPN, ping)
25+
.spawn();
26+
27+
// Keep the receiver running indefinitely
28+
loop {
29+
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
30+
}
31+
32+
}
33+
34+
async fn run_sender(ticket: EndpointTicket) -> Result<()> {
35+
// create a send side & send a ping
36+
let send_ep = Endpoint::builder().bind().await?;
37+
let send_pinger = Ping::new();
38+
let rtt = send_pinger.ping(&send_ep, ticket.endpoint_addr().clone()).await?;
39+
println!("ping took: {:?} to complete", rtt);
40+
Ok(())
41+
}
42+
43+
#[tokio::main]
44+
async fn main() -> Result<()> {
45+
let mut args = env::args().skip(1);
46+
let role = args
47+
.next()
48+
.ok_or_else(|| anyhow!("expected 'receiver' or 'sender' as the first argument"))?;
49+
50+
match role.as_str() {
51+
"receiver" => run_receiver().await,
52+
"sender" => {
53+
let ticket_str = args
54+
.next()
55+
.ok_or_else(|| anyhow!("expected ticket as the second argument"))?;
56+
let ticket = EndpointTicket::deserialize(&ticket_str)
57+
.map_err(|e| anyhow!("failed to parse ticket: {}", e))?;
58+
59+
run_sender(ticket).await
60+
}
61+
_ => Err(anyhow!("unknown role '{}'; use 'receiver' or 'sender'", role)),
62+
}
63+
}

0 commit comments

Comments
 (0)