#crdt #yrs #web-rtc #protocols #update #signaling #connector

yrs-webrtc

WebRTC network connector for Yjs/Yrs document update exchange

2 unstable releases

0.2.0 Nov 25, 2023
0.1.0 May 5, 2023

#5 in #signaling

MIT license

46KB
806 lines

yrs-webrtc

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).

Example

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),
    }
}

Dependencies

~34–49MB
~1M SLoC