Set up
We’ll assume you’ve set up Rust and cargo on your machine. Create a new project:src/main.rs.
Protocols and ALPN
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 the ALPN string to decide which handler processes the data.iroh-ping is a diagnostic protocol that lets two endpoints exchange lightweight ping/pong messages to prove connectivity and measure round-trip latency. You can build your own protocol handlers or use existing ones like iroh-ping.
To write your own protocol, see the protocol documentation page.
What is a ticket?
When an iroh endpoint comes online, it has an address containing its Endpoint ID, relay URL, and direct addresses. The address is a structured representation that other iroh endpoints can use to dial it. 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 reach the sender somehow so that endpoints can discover each other while still bootstrapping a secure, end-to-end encrypted connection. In this example we just use a string for users to copy and paste, but in your app you could publish it to a server, send it as a QR code, or pass it as a URL query parameter. It’s up to you.
For more on how this works, see Tickets and Discovery.
The receiver
The receiver creates an iroh endpoint, brings it online, prints a ticket containing its address, and runs a router that accepts incoming ping requests until you press Ctrl+C:The sender
The sender creates its own endpoint, parses the receiver’s ticket, and dials over iroh-ping:Wiring main
Parse the command-line argument to decide whether to run as receiver or sender:Run it
In one terminal, start the receiver:Connection issues? If the sender can’t reach the receiver, see the troubleshooting guide to enable detailed logging or use
iroh-doctor to diagnose network problems.Optional: send metrics to Iroh Services
If you want to see how your endpoints are performing (direct data rate, NAT traversal success, traffic volume) you can wire in Iroh Services as an optional client. Add the dependency:run_receiver (and/or run_sender), conditionally connect to Iroh Services if the IROH_SERVICES_API_SECRET environment variable is set. If the variable isn’t set the connection is skipped silently and your endpoint runs as before. If it is set, your endpoint shows up in the Iroh Services dashboard with live metrics.