Skip to main content

Crate iroh_live_rooms

Crate iroh_live_rooms 

Source
Expand description

Multi-party rooms over iroh gossip and MoQ.

A room is a gossip topic. Its members announce themselves and the names of the broadcasts they publish into the room. Rooms owns the gossip instance, and Rooms::join returns a Room. Its membership is a watched RoomState, and Room::subscribe reads a member’s broadcast on demand. The rooms know nothing of what the broadcasts carry, apart from chat, a text chat every member can publish next to its media, and Room::publish_value, a broadcast of one latest-value JSON track for state the members share, which Room::read_values reads from every member. Both readers build on Room::for_each_member, which runs a task for each member that publishes a given name, and on Room::subscriptions, which subscribes to one member’s broadcast again after each end.

A member stays in the room while its gossip lease holds. It renews the lease every thirty seconds, and drops out two minutes after the last renewal, give or take ten seconds, or at once when it leaves.

A member that goes away without leaving stays listed until its lease runs out. Its MemberConnection tells sooner: a member turns Disconnected once the last direct session between this node and it closes. A member this node never had a direct session with is Unknown, since only the lease can tell whether it is still there.

Room broadcasts are private to the members. Room::publish places a broadcast at rooms/<member>/<topic>/<name> with the membership as its audience, and a member that leaves loses the paths. Room::subscribe reads each member over the session with that member, so no other peer can stand in for it.

A room ticket is a bearer secret. Anyone who holds it can join the room, read every member’s broadcasts and publish into the room. Share it only with the people you want in the room, over a channel you trust.

The node’s policy has to let each peer publish under its RoomsLayout scope, or Room::subscribe fails. policy((LiveLayout::default(), RoomsLayout)) does that.

use iroh_live_rooms::{RoomTicket, Rooms};

let rooms = Rooms::new(&node);
// Mount the service with `rooms.mount(router_builder)`.
let room = rooms
    .join(&RoomTicket::generate(), Some("ada".into()))
    .await?;
// Fill it with tracks, or build a `LocalBroadcast` in it instead.
let _cam: moq_net::broadcast::Producer = room.publish("cam")?;

let mut state = room.state();
let room_state = state.get();
for (peer, member) in &room_state.members {
    for name in &member.broadcasts {
        let subscription = room.subscribe(*peer, name).await?;
    }
}

A member can end a broadcast and publish it again, or briefly drop this node from its membership, without RoomState changing. So watch each subscription for its end, and subscribe again while the state still lists the broadcast. Room::subscriptions does this, and for media, iroh_live::media::RoomExt::watch builds on it.

Rooms::join and Room::subscribe are cancellation safe: a dropped join leaves the topic, and a dropped subscribe leaves its dial running. Room::leave and Rooms::shutdown are not. Both are idempotent: call again to finish. Concurrent calls of Rooms::shutdown wait for the same shutdown.

Modules§

chat
Room chat: one more broadcast every member publishes into the room.

Structs§

MemberSubscriptions
The subscriptions to one member’s broadcast, one after another, from Room::subscriptions.
MemberValue
One update of a member’s value, from MemberValues.
MemberValues
The values of one track across every member, from Room::read_values.
Room
A joined room.
RoomMember
One member of a room.
RoomState
Who is in a room, and what each member publishes into it.
RoomTicket
A room: its gossip topic and the peers to contact first.
Rooms
Rooms on one endpoint.
RoomsLayout
The path layout of room broadcasts: rooms/<member>/<topic>/<name>.

Enums§

Error
An error from a room or the room service.
MemberConnection
Whether this node has a direct session with a room member.

Constants§

ALPN
The ALPN rooms speak, iroh-gossip’s.