iroh_live_rooms/lib.rs
1//! Multi-party rooms over iroh gossip and MoQ.
2//!
3//! A room is a gossip topic. Its members announce themselves and the names of
4//! the broadcasts they publish into the room. [`Rooms`] owns the gossip
5//! instance, and [`Rooms::join`] returns a [`Room`]. Its membership is a
6//! watched [`RoomState`], and [`Room::subscribe`] reads a member's broadcast
7//! on demand. The rooms know nothing of what the broadcasts carry, apart
8//! from [`chat`], a text chat every member can publish next to its media,
9//! and [`Room::publish_value`], a broadcast of one latest-value JSON track
10//! for state the members share, which [`Room::read_values`] reads from every
11//! member. Both readers build on [`Room::for_each_member`], which runs a
12//! task for each member that publishes a given name, and on
13//! [`Room::subscriptions`], which subscribes to one member's broadcast again
14//! after each end.
15//!
16//! A member stays in the room while its gossip lease holds. It renews the
17//! lease every thirty seconds, and drops out two minutes after the last
18//! renewal, give or take ten seconds, or at once when it leaves.
19//!
20//! A member that goes away without leaving stays listed until its lease runs
21//! out. Its [`MemberConnection`] tells sooner: a member turns
22//! [`Disconnected`](MemberConnection::Disconnected) once the last direct
23//! session between this node and it closes. A member this node never had a
24//! direct session with is [`Unknown`](MemberConnection::Unknown), since only
25//! the lease can tell whether it is still there.
26//!
27//! Room broadcasts are private to the members. [`Room::publish`] places a
28//! broadcast at `rooms/<member>/<topic>/<name>` with the membership as its
29//! audience, and a member that leaves loses the paths.
30//! [`Room::subscribe`] reads each member over the session with that member, so
31//! no other peer can stand in for it.
32//!
33//! A room ticket is a bearer secret. Anyone who holds it can join the room,
34//! read every member's broadcasts and publish into the room. Share it only
35//! with the people you want in the room, over a channel you trust.
36//!
37//! The node's [policy](iroh_live_node::NodeConfig::with_policy) has to let
38//! each peer publish under its [`RoomsLayout`] scope, or [`Room::subscribe`]
39//! fails.
40//! [`policy((LiveLayout::default(), RoomsLayout))`](iroh_live_node::layout::policy)
41//! does that.
42//!
43//! ```no_run
44//! # async fn example(node: iroh_live_node::Node)
45//! # -> Result<(), Box<dyn std::error::Error>> {
46//! use iroh_live_rooms::{RoomTicket, Rooms};
47//!
48//! let rooms = Rooms::new(&node);
49//! // Mount the service with `rooms.mount(router_builder)`.
50//! let room = rooms
51//! .join(&RoomTicket::generate(), Some("ada".into()))
52//! .await?;
53//! // Fill it with tracks, or build a `LocalBroadcast` in it instead.
54//! let _cam: moq_net::broadcast::Producer = room.publish("cam")?;
55//!
56//! let mut state = room.state();
57//! # use n0_watcher::Watcher;
58//! let room_state = state.get();
59//! for (peer, member) in &room_state.members {
60//! for name in &member.broadcasts {
61//! let subscription = room.subscribe(*peer, name).await?;
62//! # drop(subscription);
63//! }
64//! }
65//! # Ok(())
66//! # }
67//! ```
68//!
69//! A member can end a broadcast and publish it again, or briefly drop this
70//! node from its membership, without [`RoomState`] changing. So watch each
71//! subscription for its end, and subscribe again while the state still lists
72//! the broadcast. [`Room::subscriptions`] does this, and for media,
73//! `iroh_live::media::RoomExt::watch` builds on it.
74//!
75//! [`Rooms::join`] and [`Room::subscribe`] are cancellation safe: a dropped
76//! `join` leaves the topic, and a dropped `subscribe` leaves its dial running.
77//! [`Room::leave`] and [`Rooms::shutdown`] are not. Both are idempotent: call
78//! again to finish. Concurrent calls of [`Rooms::shutdown`] wait for the same
79//! shutdown.
80
81#![forbid(unsafe_code)]
82
83mod actor;
84pub mod chat;
85mod members;
86mod room;
87mod rooms;
88mod ticket;
89
90pub use self::{
91 members::{MemberSubscriptions, MemberValue, MemberValues},
92 room::{MemberConnection, Room, RoomMember, RoomState, RoomsLayout},
93 rooms::{ALPN, Error, Rooms},
94 ticket::RoomTicket,
95};