Expand description
Peer channels for realtime communication in webxdcs.
We use Iroh as an ephemeral peer channels provider to create direct communication channels between webxdcs. See here for the webxdc specs.
Ephemeral channels should be established lazily, to avoid bootstrapping p2p connectivity when it’s not required. Only when a webxdc subscribes to realtime data or when a reatlime message is sent, the p2p machinery should be started.
Adding peer channels to webxdc needs upfront negotiation of a topic and sharing of public keys so that nodes can connect to each other. The explicit approach is as follows:
- We introduce a new
IrohGossipTopic
message header with a random 32-byte TopicId, securely generated on the initial webxdc sender’s device. This message header is encrypted and sent in the same message as the webxdc application. - Whenever
joinRealtimeChannel().setListener()
orjoinRealtimeChannel().send()
is called by the webxdc application, we start a routine to establish p2p connectivity and join the gossip swarm with Iroh. - The first step of this routine is to introduce yourself with a regular message containing the
IrohNodeAddr
. This message contains the users relay-server and public key. Direct IP address is not included as this information can be persisted by email providers. - After the announcement, the sending peer joins the gossip swarm with an empty list of peer IDs (as they don’t know anyone yet).
- Upon receiving an announcement message, other peers store the sender’s [NodeAddr] in the database
(scoped per WebXDC app instance/message-id). The other peers can then join the gossip with
joinRealtimeChannel().setListener()
andjoinRealtimeChannel().send()
just like the other peers.
Structs§
- Channel
State 🔒 - Single gossip channel state.
- Iroh
- Store iroh peer channels for the context.
Constants§
- PUBLIC_
KEY_ 🔒LENGTH - The length of an ed25519
PublicKey
, in bytes. - PUBLIC_
KEY_ 🔒STUB
Functions§
- add_
gossip_ peer_ from_ header - Add gossip peer from
Iroh-Node-Addr
header to WebXDC message identified byinstance_id
. - create_
iroh_ 🔒header - Creates
Iroh-Gossip-Header
with a new random topic and stores the topic for the message. - create_
random_ 🔒topic - Creates a new random gossip topic.
- get_
iroh_ 🔒gossip_ peers - Get a list of [NodeAddr]s for one webxdc.
- get_
iroh_ 🔒topic_ for_ msg - Get the topic for a given MsgId.
- insert_
topic_ 🔒stub - Insert topicId into the database so that we can use it to retrieve the topic.
- iroh_
add_ 🔒peer_ for_ topic - Cache a peers [NodeId] for one topic.
- leave_
webxdc_ realtime - Leave the gossip of the webxdc with given MsgId.
- send_
webxdc_ realtime_ advertisement - Send a gossip advertisement to the chat that MsgId belongs to.
This method should be called from the frontend when
joinRealtimeChannel
is called. - send_
webxdc_ realtime_ data - Send realtime data to other peers using iroh.
- subscribe_
loop 🔒