Skip to content

Commit 8da5197

Browse files
committed
add quickstart part 2
1 parent 37dcb88 commit 8da5197

File tree

1 file changed

+34
-44
lines changed

1 file changed

+34
-44
lines changed

quickstart.mdx

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const metadata = {
1111

1212
Let's dive into iroh by building a simple peer-to-peer file transfer tool in rust!
1313

14-
## Get set up
14+
# Set up
1515

1616
We'll assume you've set up [rust](https://www.rust-lang.org/) and
1717
[cargo](https://doc.rust-lang.org/cargo/) on your machine.
@@ -31,8 +31,9 @@ cargo add iroh iroh-ping iroh-tickets tokio anyhow
3131

3232
From here on we'll be working inside the `src/main.rs` file.
3333

34+
# Part 1: Simple Ping Protocol
3435

35-
## Create an `iroh::Endpoint`
36+
### Create an Endpoint
3637

3738
To start interacting with other iroh endpoints, we need to build an `iroh::Endpoint`.
3839
This is what manages the possibly changing network underneath, maintains a
@@ -57,32 +58,20 @@ connections](https://docs.rs/iroh/latest/iroh/endpoint/struct.Endpoint.html#meth
5758
or [accept
5859
them](https://docs.rs/iroh/latest/iroh/endpoint/struct.Endpoint.html#method.accept).
5960

60-
<Note>
61-
Here, we're specifically configuring the `Endpoint`'s builder to include "number 0 discovery".
62-
This makes it connect to DNS servers that [number 0](https://n0.computer) runs to find which relay to talk to for specific `EndpointId`s.
63-
It's a great default!
64-
65-
The iroh toolkit is built to be modular and flexible, so if you want to use a different discovery mechanism, you can!
66-
67-
If all of this is too much magic for your taste, it's possible for the endpoint to work entirely without any discovery services.
68-
In that case, you'll need to make sure you're not only dialing by `EndpointId`, but also help the `Endpoint` out with giving it the whole [`EndpointAddr`](https://docs.rs/iroh/latest/iroh/struct.EndpointAddr.html) when connecting.
69-
</Note>
70-
7161

72-
## Protocols
62+
### Protocols
7363

74-
Now that we have an `Endpoint`, we can start using protocols over it. A protocol
75-
is a specific way of exchanging messages over a connection.
64+
Now that we have an `Endpoint`, we can start using protocols over it.
7665

77-
Similar to how HTTP is a protocol for exchanging web pages, iroh protocols define
78-
ways to exchange data over iroh connections.
66+
A **protocol** defines how two endpoints exchange messages. Just like HTTP
67+
defines how web browsers talk to servers, iroh protocols define how peers
68+
communicate over iroh connections.
7969

80-
Each endpoint can support multiple protocols, identified by a string called
81-
an ALPN string. This just tells the program which handler to use for
82-
processing data as it arrives.
70+
Each protocol is identified by an **ALPN** (Application-Layer Protocol
71+
Negotiation) string. When a connection arrives, the router uses this string to
72+
decide which handler processes the data.
8373

84-
When building applications with iroh, you can either build your own custom
85-
protocol handlers, or use existing ones.
74+
You can build custom protocol handlers or use existing ones like `iroh-ping`.
8675

8776
<Note>
8877
Learn more about writing your own protocol on the [protocol documentation page](/protocols/writing-a-protocol).
@@ -103,16 +92,16 @@ use iroh_ping::Ping;
10392
async fn main() -> anyhow::Result<()> {
10493
// Create an endpoint, it allows creating and accepting
10594
// connections in the iroh p2p world
106-
let recv_ep = Endpoint::builder().bind().await?;
95+
let endpoint = Endpoint::builder().bind().await?;
10796

10897
// bring the endpoint online before accepting connections
109-
recv_ep.online().await;
98+
endpoint.online().await;
11099

111100
// Then we initialize a struct that can accept ping requests over iroh connections
112101
let ping = Ping::new();
113102

114103
// receiving ping requests
115-
let recv_router = Router::builder(recv_ep)
104+
let recv_router = Router::builder(endpoint)
116105
.accept(iroh_ping::ALPN, ping)
117106
.spawn();
118107

@@ -139,13 +128,13 @@ use iroh_ping::Ping;
139128
async fn main() -> Result<()> {
140129
// Create an endpoint, it allows creating and accepting
141130
// connections in the iroh p2p world
142-
let recv_ep = Endpoint::builder().bind().await?;
131+
let endpoint = Endpoint::builder().bind().await?;
143132

144133
// Then we initialize a struct that can accept ping requests over iroh connections
145134
let ping = Ping::new();
146135

147136
// receiving ping requests
148-
let recv_router = Router::builder(recv_ep)
137+
let recv_router = Router::builder(endpoint)
149138
.accept(iroh_ping::ALPN, ping)
150139
.spawn();
151140

@@ -163,7 +152,7 @@ async fn main() -> Result<()> {
163152
```
164153

165154

166-
### Getting round trip time
155+
### Running it
167156

168157
Now that we've created both the sending and receiving sides of our ping program, we can run it!
169158

@@ -172,13 +161,13 @@ cargo run
172161
```
173162

174163
```bash
175-
I Compiling ping-quickstart v0.1.0 (/Users/raemckelvey/dev/ping-quickstart)
164+
I Compiling ping-pong v0.1.0 (/dev/ping-pong)
176165
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.62s
177-
Running `target/debug/ping-quickstart`
166+
Running `target/debug/ping-pong`
178167
ping took: 1.189375ms to complete
179168
```
180169

181-
## Part 2: Separate Sender and Receiver
170+
## Part 2: Discovering Peers with Tickets
182171

183172
Round-trip time isn't very useful when both roles live in the same binary
184173
instance. Let's split the app into two subcommands so you can run the receiver
@@ -188,7 +177,7 @@ on one machine and the sender on another.
188177

189178
When an iroh endpoint comes online, it has an address containing its node ID,
190179
relay URL, and direct addresses. An `EndpointTicket` wraps this address into a
191-
serializable ticketa short string you can copy and paste. Share this ticket
180+
serializable ticket -- a short string you can copy and paste. Share this ticket
192181
with senders so they can dial the receiver without manually exchanging
193182
networking details.
194183

@@ -197,11 +186,11 @@ A ticket is made from an endpoint's address like this:
197186
```rust
198187
use iroh_tickets::{Ticket, endpoint::EndpointTicket};
199188

200-
let ticket = EndpointTicket::new(recv_ep.addr());
189+
let ticket = EndpointTicket::new(endpoint.addr());
201190
println!("{ticket}");
202191
```
203192

204-
For more details on tickets, see [Discovery](/concepts/discovery).
193+
For more details on how it works, see [Tickets](/concepts/tickets) and [Discovery](/concepts/discovery).
205194

206195
### Receiver
207196

@@ -217,15 +206,15 @@ use iroh_tickets::{Ticket, endpoint::EndpointTicket};
217206
use std::env;
218207

219208
async fn run_receiver() -> Result<()> {
220-
let recv_ep = Endpoint::builder().bind().await?;
221-
recv_ep.online().await;
209+
let endpoint = Endpoint::builder().bind().await?;
210+
endpoint.online().await;
222211

223212
let ping = Ping::new();
224213

225-
let ticket = EndpointTicket::new(recv_ep.addr());
214+
let ticket = EndpointTicket::new(endpoint.addr());
226215
println!("{ticket}");
227216

228-
Router::builder(recv_ep)
217+
Router::builder(endpoint)
229218
.accept(iroh_ping::ALPN, ping)
230219
.spawn();
231220

@@ -296,13 +285,14 @@ cargo run -- sender <TICKET>
296285

297286
You should see the round-trip time printed by the sender.
298287

299-
## That's it!
288+
## More Tutorials
300289

301290
You've now successfully built a small tool for sending messages over a peer to peer network! 🎉
302291

303-
The full example with the very latest version of iroh and iroh-ping can be [viewed on github](https://github.com/n0-computer/iroh-blobs/blob/main/examples/transfer.rs).
292+
The full example with the very latest version of iroh and iroh-ping can be
293+
[viewed on github](https://github.com/n0-computer/iroh-ping).
304294

305295
If you're hungry for more, check out
306-
- the [iroh rust documentation](https://docs.rs/iroh),
307-
- Blob storage with [iroh-blobs](/protocols/blobs),
308-
- [other examples](/examples)
296+
- [Blob storage with iroh-blobs](/protocols/blobs)
297+
- [Build your own P2P Chat app tutorial](/examples/chat)
298+
- [iroh rust documentation](https://docs.rs/iroh)

0 commit comments

Comments
 (0)