Set up
We’ll assume you’ve set up rust and cargo on your machine. Initialize a new project by running:src/main.rs file.
Part 1: Simple Ping Protocol
Create an Endpoint
To start interacting with other iroh endpoints, we need to build aniroh::Endpoint.
This is what manages the possibly changing network underneath, maintains a
connection to the closest relay, and finds ways to address devices by
EndpointId.
Protocols
Now that we have anEndpoint, we can start using protocols over it.
A protocol defines how two endpoints exchange messages. Just like HTTP
defines how web browsers talk to servers, iroh protocols define how peers
communicate over iroh connections.
Each protocol is identified by an ALPN (Application-Layer Protocol
Negotiation) string. When a connection arrives, the router uses this string to
decide which handler processes the data.
You can build custom protocol handlers or use existing ones like iroh-ping.
Learn more about writing your own protocol on the protocol documentation page.
Ping: Receive
iroh-ping is a diagnostic protocol bundled with iroh that lets two endpoints exchange lightweight ping/pong messages to prove connectivity, measure round-trip latency, or whatever else you want to build on top of it, without building a custom handler.Endpoint.
Ping: Send
At this point what we want to do depends on whether we want to accept incoming iroh connections from the network or create outbound iroh connections to other endpoints. Which one we want to do depends on if the executable was called withsend as
an argument or receive, so let’s parse these two options out from the CLI
arguments and match on them:
Running it
Now that we’ve created both the sending and receiving sides of our ping program, we can run it!Part 2: Discovering Peers with Tickets
Round-trip time isn’t very useful when both roles live in the same binary instance. Let’s split the app into two subcommands so you can run the receiver on one machine and the sender on another.What is a ticket?
When an iroh endpoint comes online, it has an address containing its node ID, relay URL, and direct addresses. AnEndpointTicket wraps this address into a
serializable ticket — a short string you can copy and paste. Share this ticket
with senders so they can dial the receiver without manually exchanging
networking details.
A ticket is made from an endpoint’s address like this: