Guides
Publish through a media relay
A publisher that viewers cannot reach directly pushes its broadcasts to an media relay over a relay link. Viewers then read them from the media relay. iroh relays and media relays explains when this helps.
In short
irl publish --relay <RELAY_ENDPOINT_ID>
use iroh::EndpointId;
use iroh_live::{Node, RelayConfig, layout::LiveLayout, media::LocalBroadcast};
fn run(node: &Node, layout: &LiveLayout, relay: EndpointId) -> Result<(), Box<dyn std::error::Error>> {
// Pushes the node's publications to the media relay, and redials when it drops.
let link = node.attach_relay(RelayConfig::iroh(relay).with_consume(false))?;
let broadcast: LocalBroadcast = layout.publish(node, "pattern")?;
drop((link, broadcast));
Ok(())
}
With irl
irl publish --relay <RELAY_ENDPOINT_ID>
The media relay prints its endpoint id on start. irl publish stays attached and
redials if the session drops, and prints the path at which viewers find the
broadcast on the media relay, live/<publisher id>/<name>. With --no-serve it
accepts no direct viewers and prints no ticket.
A media relay that only browsers read needs no push: it can pull a broadcast by its ticket, see Watch in a browser.
From Rust
Node::attach_relay returns a RelayLink. Every publication with the
Granted audience, which LiveLayout::publish uses, is then visible to the
media relay, and a Peers one never is. A publisher that does not need the media relay’s
other routes sets with_consume(false).
use iroh::{EndpointId, protocol::Router};
use iroh_live::{
EndpointConfig, Node, RelayConfig,
layout::LiveLayout,
media::{LocalBroadcast, MediaEncoding, spec::MediaSpec},
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let relay: EndpointId = std::env::args()
.nth(1)
.ok_or("usage: relay <relay id>")?
.parse()?;
let layout = LiveLayout::default();
let endpoint = EndpointConfig::from_env()?.bind().await?;
let node = Node::new(endpoint.clone(), layout.config());
// Direct viewers still dial in. A relay-only publisher can skip the router
// and close the endpoint itself.
let router = node.mount(Router::builder(endpoint)).spawn();
let link = node.attach_relay(RelayConfig::iroh(relay).with_consume(false))?;
let broadcast: LocalBroadcast = layout.publish(&node, "pattern")?;
broadcast.set_spec(&MediaSpec::test(), MediaEncoding::default()).await?;
println!("{}", layout.ticket(node.id(), "pattern")?);
tokio::signal::ctrl_c().await?;
link.detach().await;
router.shutdown().await?;
Ok(())
}
RelayLink::status() watches whether the link is connected. A media relay started
with --auth-key accepts a JWT, which the link presents through
RelayConfig::with_token. See Use tokens.
Reading from a media relay
A viewer with a media relay attached with the default config reaches a
broadcast on it with Reach::Relays, or with Reach::Both(publisher) to
also try a direct session:
use iroh::EndpointId;
use iroh_live::{Node, Reach, RelayConfig, Subscription, node::RelayLink};
async fn read_via_relay(
node: &Node,
relay: EndpointId,
path: &str,
) -> Result<(RelayLink, Subscription), Box<dyn std::error::Error>> {
// Stays attached until `detach` or the node shuts down.
let link = node.attach_relay(RelayConfig::iroh(relay))?;
let subscription = node.subscribe(path, Reach::Relays).await?;
Ok((link, subscription))
}
This is how a Rust node reads a broadcast that a browser published on the
media relay, as iroh-live/examples/subscribe_test.rs does. irl watch cannot,
since its tickets name a publisher, not a media relay.