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 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.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!Having trouble? If your ping isn’t working or you want to see what’s happening under the hood, check out our troubleshooting guide to enable logging and diagnose issues.
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. An address is a structured representation of this information that can be consumed by iroh endpoints to dial each other. AnEndpointTicket wraps this address into a serializable format — a short
string you can copy and paste. Share this string with senders so they can dial
the receiver without manually exchanging networking details.
This out of band information must be sent between the endpoints so that they can
discover and connect to each other, while maintaining security bootstrapping the
end-to-end encrypted connection. In this example, we will just use a string for
users to copy/paste, but in your app, you could publish it to a server or send
it as a QR code or as a query string at the end of a URL. It’s up to you.
A ticket is made from an endpoint’s address like this:
Receiver
The receiver creates an endpoint, brings it online, prints its ticket, then runs a router that accepts incoming ping requests indefinitely:Sender
The sender parses the ticket, creates its own endpoint, and pings the receiver’s address:Wiring it together
Parse the command-line arguments to determine whether to run as receiver or sender:Running it
In one terminal, start the receiver:Connection issues? If the sender can’t reach the receiver or you’re seeing errors, visit our troubleshooting guide to enable detailed logging and use
iroh-doctor to diagnose network problems.