Wrtc
Wrapper around webrtc-rs library, focused on developer experience
Install / Use
/learn @Horusiath/WrtcREADME
wrtc
This is a Rust library wrapper around popular webrtc, that's focused on a developer experience.
Message exchange protocol is compatible with NPM simple-peer library and tries its best to conform to idiomatic Rust structures eg. futures Sink/Stream instead of callbacks.
API coverage
Current state only covers data channels.
- [x] WebRTC
PeerConnection - [x] data channels
- [ ] streams
- [ ] transceivers
Example
#[tokio::main]
async fn main() -> Result<(), Error> {
// we use default options with "test" data channel to be initialized
let options = Options::with_channels(&["test"]);
// setup first peer as a connection initiator
let p1 = Arc::new(PeerConnection::start(true, options.clone()).await?);
// setup second peer as a connection acceptor
let p2 = Arc::new(PeerConnection::start(false, options).await?);
// WebRTC requires offer/answer roundtrip between peers to be send
// outside of the WebRTC protocol itself. It's usually done via another
// medium (HTTP, Web Sockets etc.). Here we just send them straight
// over the memory.
let _ = exchange(p1.clone(), p2.clone());
let _ = exchange(p2.clone(), p1.clone());
// wait for connection negotiation to complete
p1.connected().await?;
p2.connected().await?;
{
// Get data channel references on both ends. In `wrtc`,
// `DataChannel` implements both futures Sink and Stream.
let mut dc1 = p1.data_channels().next().await.unwrap();
let mut dc2 = p2.data_channels().next().await.unwrap();
// make sure that channels have been established
dc1.ready().await?;
dc2.ready().await?;
// send message from peer 1 to peer 2
let data: Bytes = "hello".into();
dc1.send(data.clone()).await?;
let msg = dc2.next().await.unwrap()?;
assert_eq!(msg.data, data);
}
// gracefully close peers
p1.close().await?;
p2.close().await?;
Ok(())
}
fn exchange(
from: Arc<PeerConnection>,
to: Arc<PeerConnection>,
) -> JoinHandle<Result<(), Error>> {
tokio::spawn(async move {
while let Some(signal) = from.listen().await {
to.signal(signal).await?;
}
Ok(())
})
}
Related Skills
node-connect
337.7kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.3kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
337.7kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.3kCommit, push, and open a PR
