Crates.io | yrs-webrtc |
lib.rs | yrs-webrtc |
version | 0.2.0 |
source | src |
created_at | 2023-05-05 10:53:09.936523 |
updated_at | 2023-11-25 02:26:27.587179 |
description | WebRTC network connector for Yjs/Yrs document update exchange |
homepage | https://github.com/Horusiath/yrs-webrtc |
repository | https://github.com/Horusiath/yrs-webrtc |
max_upload_size | |
id | 857605 |
size | 175,930 |
This is a Yrs integrated network provider which makes use of WebRTC protocol to propagate changes. It's compatible with y-webrtc JavaScript client and makes use of the same signaling server protocol (there are Node.js and Rust implementations of that protocol for you to use).
use yrs_webrtc::signal::PeerEvent;
use yrs_webrtc::{Error, Room, SignalingConn};
// start a WebRTC peer
async fn peer(topic: &str) -> Result<(), Error> {
// create a signaling client
let conn = Arc::new(SignalingConn::connect("ws://localhost:8000/signaling").await?);
// create a new peer collaborating on a given topic
let room = Room::open(topic, Awareness::default(), [conn]);
let mut peer_events = room.peer_events().subscribe();
// wait for room to initialize connection to signaling server
room.connect().await?;
// listen for events about connecting/disconnecting peers
let _ = tokio::spawn(async move {
while let Ok(e) = peer_events.recv().await {
println!("received peer event: {e:?}");
}
});
// watch for incoming updates
let _ = {
let awareness = room.awareness().write().await;
awareness.doc().observe_update_v1(move |txn, u| {
let u = Update::decode_v1(&u.update).unwrap();
println!("received update: {u:?}");
})
};
// keep room alive and close it when you're done
room.close().await?;
}
// create y-webrtc/yrs-webrtc compatible signaling server
use yrs_warp::signaling::{signaling_conn, SignalingService};
#[tokio::main]
async fn main() -> Result<(), Error> {
let signaling = SignalingService::new();
let ws = warp::path("signaling")
.and(warp::ws())
.and(warp::any().map(move || signaling.clone()))
.and_then(ws_handler);
warp::serve(ws).run(([0, 0, 0, 0], 8000)).await;
Ok(())
}
async fn ws_handler(ws: Ws, svc: SignalingService) -> Result<impl Reply, Rejection> {
Ok(ws.on_upgrade(move |socket| ws_conn(socket, svc)))
}
async fn ws_conn(ws: WebSocket, svc: SignalingService) {
match signaling_conn(ws, svc).await {
Ok(_) => println!("signaling connection stopped"),
Err(e) => eprintln!("signaling connection failed: {}", e),
}
}