Guides
Run a room
A room holds several members over a gossip topic. Each member publishes into the room and plays the others. Calls and rooms explains the model.
In short
irl room --display-name alice
irl room --display-name bob <TICKET>
use iroh_live::{RoomsNode, media::{LocalBroadcast, RoomExt}, rooms::RoomTicket};
async fn run(live: &RoomsNode) -> Result<(), Box<dyn std::error::Error>> {
let room = live.rooms.join(&RoomTicket::generate(), Some("ada".into())).await?;
println!("room ticket: {}", room.ticket());
// The members read it. Fill it with sources as in any broadcast.
let broadcast: LocalBroadcast = room.publish("cam")?;
drop(broadcast);
Ok(())
}
With irl
irl room --display-name alice # opens a new room, prints its ticket
irl room --display-name bob <TICKET> # joins it
The window shows the others in a grid and has a chat panel. Pass on the ticket that the newest window prints. That ticket lists the newest member as a bootstrap member.
Warning
A room ticket is a secret. Anyone who has it can join the room and watch its members, so share it only with the people you invite.
From Rust
Rooms need the rooms feature. RoomsNode::bind binds an endpoint and
sets up a node for rooms on it. The node’s config takes the union of the live
layout and RoomsLayout, so members publish under their room paths, and the
router accepts both the node’s sessions and the rooms’ gossip.
RoomsNode::shutdown leaves every joined room, which tells the other members
at once, and then shuts the node and the router down. A room stops with its
last Room handle and tells nobody, so keep one until the shutdown.
This program creates a room, prints its ticket, publishes the test pattern with its beeps, and plays the first broadcast another member announces. Pass a ticket to join instead.
use iroh::Watcher;
use iroh_live::{
EndpointConfig, RoomsNode,
media::{LocalBroadcast, MediaEncoding, PlayerConfig, RoomExt, spec::MediaSpec},
rooms::RoomTicket,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Members dial in over gossip and moq.
let live = RoomsNode::bind(EndpointConfig::from_env()?).await?;
let ticket = match std::env::args().nth(1) {
Some(ticket) => ticket.parse()?,
None => RoomTicket::generate(),
};
let room = live.rooms.join(&ticket, Some("ada".into())).await?;
println!("room ticket: {}", room.ticket());
let broadcast: LocalBroadcast = room.publish("cam")?;
broadcast.set_spec(&MediaSpec::test(), MediaEncoding::default()).await?;
let mut state = room.state();
let (peer, name) = loop {
let found = state.get().members.iter().find_map(|(peer, member)| {
member
.broadcasts
.iter()
.next()
.map(|name| (*peer, name.clone()))
});
if let Some(found) = found {
break found;
}
state.updated().await?;
};
let player = room.watch(peer, &name).play(PlayerConfig::default())?;
let mut frames = player.video_frames();
while let Some(frame) = frames.next().await {
println!("{peer} {name}: {}", frame.size());
}
live.shutdown().await;
Ok(())
}
Only an application whose config includes RoomsLayout, as RoomsNode’s
does, lets members publish room broadcasts. The rooms feature alone grants
nothing. An application that builds its own node and router composes the
layouts with layout::config((LiveLayout::default(), RoomsLayout)), creates
the service with Rooms::new(&node), and mounts it with Rooms::mount next
to Node::mount. A node that joins a room without Rooms::mount logs a
warning, since the other members cannot reach its gossip.
Membership
Room::state() watches a RoomState: every other member, its display name,
the names of its broadcasts, and its connection. A slow reader sees the
latest state and no backlog. To show “joined” and “left”, compare two states.
RoomState::member_name(id) gives the name to show for a member: its display name,
or its short endpoint id when it announced none or is no longer listed.
A member renews its announcement every 30 seconds, and its lease runs out two
minutes after the last renewal. Other members check for expired leases every
10 seconds. So a member that stops renewing drops out 2:00 to 2:10 after its
last renewal, which is 2:00 to 2:40 after it went away. A member that calls
Room::leave() drops out at once. Lease times use the
member’s own clock, so a member whose clock is more than two minutes behind
is never seen.
A member that crashes or loses its network stays listed until its lease runs
out. RoomMember::connection tells sooner whether the member is still there.
It reads the node’s direct sessions, dialed or accepted, and has three states:
Connected: a direct session with the member is open.Disconnected { since }: the last direct session with the member closed, and none is open. A new session makes the memberConnectedagain.Unknown: this node had no direct session with the member while it was listed. This is the case for a member that nobody subscribed to in either direction, and for one reached only through a media relay.
A member whose process exits closes its sessions, so it turns Disconnected
at once. A crash or a network loss closes nothing, so the session ends only
after the QUIC idle timeout, 30 seconds by default. A session that this node
closes itself also counts.
For a grid, stop retrying a Disconnected member’s broadcasts, or show its
tile as gone, and keep Unknown members as they are. Gossip still removes the
member when its lease runs out.
Publishing and subscribing
Room::publish(name) puts the broadcast at rooms/<endpoint id>/<topic>/<name>, where only the room’s members can read it. A member that
leaves or expires loses the path at once. Room::unpublish(name), or closing
the broadcast, removes the name from this member’s announcement. A name must
not be empty or contain a slash.
Room::publish_value::<T>(name, track) publishes state the members share,
such as presence or a game, as a broadcast with one latest-value JSON track
and no media catalog. Room::read_values::<T>(name, track) reads it from
every member that publishes it, as MemberValue { member, value } updates,
with None once a member stops. It reads a member again when its
subscription ends, skips a value that does not decode, and stops reading a
member whose broadcast has no such track. For one member,
room.subscribe(peer, name).await?.value_track::<T>(track) reads the track
directly. The broadcast leaves the room when the last clone of its
ValueWriter drops.
Room::subscribe(peer, name) dials the member if needed, and reads the
broadcast over the session with that member, so no other peer can stand in
for it. It waits until the member announces the name, so bound the wait with
a timeout. Leaving the room does not close subscriptions.
Room::subscriptions(peer, name) subscribes to one member’s broadcast, and
again after each end of the subscription while the member lists the name,
such as when the member restarts its broadcast or the session with it fails.
After a subscription that failed or closed early, it waits first, from two
seconds up to sixteen, since a member that vanished stays listed for minutes.
It ends once the member stops listing the name.
Room::for_each_member(name, task) runs a task for each member that
publishes name, aborts it once the member no longer does, and returns the
handle that keeps the tasks running. read_values and the chat build on it
and on subscriptions, and an application can read its own tracks the same
way. None of these keep the room joined: they stop once its last Room
handle drops.
For media, room.watch(peer, name), from iroh_live::media::RoomExt, returns
a RemoteBroadcast at once, without waiting for the member. A member can end
a broadcast and publish it again before its announcement changes, and a
session with it can fail, while the room state stays the same. The watched
broadcast follows the member through Room::subscriptions, and its players
switch to each new subscription. It closes once the member stops listing the
name, or the room is left, so a grid drops the tiles whose broadcast closed.
A member that never lists the name leaves the watched broadcast waiting.
RemoteBroadcast::catalog_missing tells a broadcast without a media catalog,
such as one of publish_value, apart from one whose catalog is late.
iroh-live-cli/src/room.rs does this.
Limits
- Rooms have no admission of their own. The ticket is a bearer secret: anyone who has it can join, announce itself, and read the members’ room broadcasts. Share it only with the people you invite.
- Every member that shows every other member subscribes to all of them, so rooms suit small groups.
- If every member listed in a ticket is offline, joining succeeds, but the room stays empty until another member reaches this one.
A room carries only broadcasts. irl room sends chat as one more broadcast
named chat, whose chat track is a JSON window of the last ten seconds of
messages, the format @moq/room reads.
Where to go next
- Watch a broadcast and render it draws each member’s frames.
- Make a one-to-one call for two people.