Keyboard shortcuts

Press ← or → to go to the previous or next page

Press S or / to search the docs

Press ? to show this help

Press Esc to hide this help

Advanced

Publish at your own path

This page publishes at a path of your own choosing, to a chosen set of peers. LiveLayout publishes at live/<endpoint id>/<name> to every granted session. For anything else, use Node::publish and Node::subscribe directly. Paths, layouts and tickets explains the idea.

In short

use iroh::EndpointId;
use iroh_live::{Audience, Node, media::LocalBroadcast};
fn run(node: &Node, other: EndpointId) -> Result<(), Box<dyn std::error::Error>> {
// A path of your own, visible to `other` only.
let path = format!("stage/{}/main", node.id());
let publication = node.publish(&path, Audience::peers([other]))?;
let broadcast = LocalBroadcast::new_in(publication.broadcast().clone())?;
drop((publication, broadcast));
Ok(())
}

The program

Node::publish(path, audience) takes any path and any audience, and Node::subscribe(path, reach) any path. Audience::peers limits who sees the broadcast, and moq enforces it on each live session. Nothing here is a ticket: the subscriber needs the publisher’s id and the path.

The subscriber’s node also has to let the publisher announce that path. The live layout allows only live/<endpoint id>/, so this program adds a second layout, stage/<endpoint id>/, and gives the node the union of both. Run stage publish <viewer endpoint id> on one machine and stage watch <publisher endpoint id> on the other. Each prints its own id on start.

use iroh::{EndpointId, protocol::Router};
use iroh_live::{
    Audience, EndpointConfig, Node, Reach,
    layout::{self, Layout, LiveLayout},
    media::{LocalBroadcast, MediaEncoding, PlayerConfig, SubscriptionExt, spec::MediaSpec},
    moq_net::{Prefix, Prefixes},
};

/// Puts a peer's broadcasts at `stage/<peer>/<name>`.
#[derive(Debug, Clone)]
struct Stage;

impl Layout<EndpointId> for Stage {
    fn scope(&self, peer: &EndpointId) -> Prefixes {
        let prefix: Prefix = format!("stage/{peer}").parse().expect("a valid prefix");
        Prefixes::from(prefix)
    }

    fn owner(&self, path: &str) -> Option<EndpointId> {
        let (peer, name) = path.strip_prefix("stage/")?.split_once('/')?;
        let id: EndpointId = peer.parse().ok()?;
        (id.to_string() == peer && !name.is_empty()).then_some(id)
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let endpoint = EndpointConfig::from_env()?.bind().await?;
    let config = layout::config((LiveLayout::default(), Stage));
    let node = Node::new(endpoint.clone(), config);
    let router = node.mount(Router::builder(endpoint)).spawn();
    let me = node.id();
    println!("our id: {me}");

    let mut args = std::env::args().skip(1);
    let (mode, other) = (args.next(), args.next());
    let other: EndpointId = other
        .ok_or("usage: stage <publish|watch> <other id>")?
        .parse()?;
    match mode.as_deref() {
        Some("publish") => {
            // Visible to `other` only.
            let audience = Audience::peers([other]);
            let publication = node.publish(&format!("stage/{me}/main"), audience)?;
            let broadcast = LocalBroadcast::new_in(publication.broadcast().clone())?;
            broadcast.set_spec(&MediaSpec::test(), MediaEncoding::default()).await?;
            tokio::signal::ctrl_c().await?;
        }
        _ => {
            let path = format!("stage/{other}/main");
            let subscription = node.subscribe(&path, Reach::Direct(other)).await?;
            let player = subscription.remote_broadcast().play(PlayerConfig::default())?;
            let mut frames = player.video_frames();
            while let Some(frame) = frames.next().await {
                println!("frame {}", frame.size());
            }
        }
    }
    router.shutdown().await?;
    Ok(())
}

A third node that dials the publisher cannot resolve the path, since the audience leaves it out.

Changing the audience

To change the set later, publish with Audience::Peers(set) and keep a clone of the n0_watcher::Watchable set. A peer taken out of the set loses the path at once. Publication::set_audience replaces the audience of a running publication.

Node::publish_with(path, audience, fill) fills the broadcast before it is announced, so a reader never sees it empty. With LiveLayout::path(id, name) it publishes under the live layout to another audience.

Where to go next