#wasm #webrtc #datachannel #peer-to-peer #webassembly


Easy-to-use wrapper for WebRTC DataChannels peer-to-peer connections written in Rust and compiling to WASM

3 releases

0.3.2 Feb 7, 2022
0.3.1 Feb 7, 2022
0.3.0 Feb 7, 2022

#716 in WebAssembly

40 downloads per month
Used in 2 crates


92 lines


This crate provides an easy-to-use wrapper around WebRTC and DataChannels for a peer-to-peer connections.


As creator of agar.io famously stated WebRTC is hard. This library aims to help, by abstracting away all the setup, and providing a simple way to send and receive messages over the data channel.

It's as easy as providing address to a signaling server instance from accompanying crate and specifying two callbacks. One for when a connection opens, and one for when a message is received. After that you can send messages back and forth without worrying about the implementation details.

Library contains three network topologies, one-to-one, which creates an equal connection between two peers, one-to-many, which specifies a host and arbitrary number of clients and many-to-many that creates connection for each pair of peers and allows sending messages to any of them.


This example shows two peers sending ping and pong messages to each other.

use wasm_peers::ConnectionType;
use wasm_peers::one_to_one::NetworkManager;
use web_sys::console;

// there must be a signaling server from accompanying crate running on this port
const SIGNALING_SERVER_URL: &str = "ws://";

fn main() {
  // there must be some mechanism for exchanging session ids between peers
  let session_id = SessionId::new("some-session-id".to_string());
  let mut peer1 = NetworkManager::new(

  let peer1_clone = peer1.clone();
  let peer1_on_open = move || peer1_clone.send_message("ping!").unwrap();
  let peer1_on_message = {
    move |message| {
      console::log_1(&format!("peer1 received message: {}", message).into());
  peer1.start(peer1_on_open, peer1_on_message).unwrap();

  let mut peer2 = NetworkManager::new(
  let peer2_on_open = || { /* do nothing */ };
  let peer2_clone = peer2.clone();
  let peer2_on_message = {
    let peer2_received_message = peer2_received_message.clone();
    move |message| {
      console::log_1(&format!("peer2 received message: {}", message).into());
  peer2.start(peer2_on_open, peer2_on_message).unwrap();

For examples for other topologies check out the docs.

For a more advanced example check out "production ready" app built with this library: Live Document.


Tomasz Karwowski

Version History

  • 0.3
    • Initial release to the public


This project is licensed under either of


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


These projects helped me grasp WebRTC in Rust:

Also, special thanks to the guys with whom I did my B.Eng. thesis.


~32K SLoC