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

Overview

wasm-peers

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

Overview

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.

Example

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://0.0.0.0:9001/one-to-one";

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(
    SIGNALING_SERVER_URL,
    session_id.clone(),
    ConnectionType::Stun,
  ).unwrap();

  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(
    SIGNALING_SERVER_URL,
    session_id,
    ConnectionType::Stun,
  ).unwrap();
  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_clone.send_message("pong!").unwrap();
    }
  };
  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.

Authors

Tomasz Karwowski
LinkedIn

Version History

  • 0.3
    • Initial release to the public

License

This project is licensed under either of

Contribution

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.

Acknowledgments

These projects helped me grasp WebRTC in Rust:

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

You might also like...
All-batteries included GStreamer WebRTC producer

webrtcsink All-batteries included GStreamer WebRTC producer, that tries its best to do The Right Thing™. Use case The webrtcbin element in GStreamer i

A fast, stable, efficient, and lightweight intranet penetration, port forwarding tool supports multiple connections, cascading proxy, and transmission encryption
A fast, stable, efficient, and lightweight intranet penetration, port forwarding tool supports multiple connections, cascading proxy, and transmission encryption

A fast, stable, efficient, and lightweight intranet penetration, port forwarding tool supports multiple connections, cascading proxy, and transmission encryption

Drop-in proxy for Discord gateway connections and sessions allowing for zero downtime deploys

gateway-proxy This is a very hacky project, so it might stop working if Discord changes their API core. This is unlikely, but keep that in mind while

Reliable p2p network connections in Rust with NAT traversal
Reliable p2p network connections in Rust with NAT traversal

Reliable p2p network connections in Rust with NAT traversal. One of the most needed libraries for any server-less / decentralised projects

Reliable p2p network connections in Rust with NAT traversal
Reliable p2p network connections in Rust with NAT traversal

Reliable p2p network connections in Rust with NAT traversal. One of the most needed libraries for any server-less, decentralised project.

An easy-to-use tunnel to localhost built in Rust. An alternative to ngrok and frp.

rslocal English | 中文 What is rslocal? Rslocal is like ngrok built in Rust, it builds a tunnel to localhost. Project status support http support tcp su

Fast and easy-to-use event-driven network library.
Fast and easy-to-use event-driven network library.

message-io is a fast and easy-to-use event-driven network library. The library handles the OS socket internally and offers a simple event message API

easy to use version controll

Verzcon To start off you need to know are you the server or the client Server If you are the server then you should run verzcon --host then it will ma

Drpc-Correct, high performance, robust, easy use Remote invocation framework
Drpc-Correct, high performance, robust, easy use Remote invocation framework

Drpc - Correct, high performance, robust, easy use Remote invocation framework

Comments
  • Comparison to Matchbox

    Comparison to Matchbox

    https://github.com/johanhelsing/matchbox: "Painless peer-to-peer WebRTC networking for rust wasm"

    Also is there any way to use a custom signalling service?

    opened by iMplode-nZ 5
  • Whether to base on libp2p,

    Whether to base on libp2p,

    I am new to socket, and I am learning about video, including WebrTC I think wasm, WebRTC, Rust are all great things. But at the same time, I discovered libp2p.

    A modular network stack.
    Run your network applications free from runtime and address services, independently of their location.
    

    I noticed that they seem to be adding webRTC (by the way, webRTC seems to be implemented on libp2p js)

    • webrtc/: Add libp2p WebRTC specification https://github.com/libp2p/specs/pull/412
    • rust implementation https://github.com/libp2p/rust-libp2p/pull/2622

    And now I see that it is going to reference https://github.com/webrtc-rs/webrtc ps, I didn't know webrtc-rs before

    So,

    1. what is wasm-peer different from WebRTC
    2. Do you want to implement wasm-peer on libp2p?
    opened by huang12zheng 3
  • One to many doesn't work.

    One to many doesn't work.

    Hi, i tried using the library using one to many. Server sees the connections and gets the session join event from the clients but not the host.

    10:47:27 [INFO] new user connected: UserId(30) <-- host
    10:47:47 [INFO] new user connected: UserId(31) <-- client
    10:47:47 [INFO] message received from user UserId(31): SessionJoin(SessionId("id"), false)
    

    no error is returned.

    bug 
    opened by Wicpar 2
A library for easily creating WebRTC data channel connections in Rust

Cyberdeck A library for easily creating WebRTC data channel connections in Rust.

RICHΛRD ΛNΛYΛ 34 Nov 10, 2022
IDP2P is a peer-to-peer identity protocol which enables a controller to create, manage and share its own proofs as well as did documents

IDP2P Experimental, inspired by ipfs, did:peer and keri Background See also (related topics): Decentralized Identifiers (DIDs) Verifiable Credentials

null 5 Oct 31, 2022
Peer-to-peer communications library for Rust based on QUIC protocol

qp2p Crate Documentation MaidSafe website SAFE Dev Forum SAFE Network Forum Overview This library provides an API to simplify common tasks when creati

MaidSafe 337 Dec 14, 2022
Simple Peer-to-Peer Exchange

Near Cetificate Devoloper - Demo Simple Peer-to-Peer Exchange On NEAR How it works? See how p2p exchange work here. Exploring The Code The contract co

null 3 Dec 20, 2021
Peer-to-peer overlay routing

Rust_Pinecone This is a port of the peer-to-peer overlay routing mechanism Pinecone and aims to be interoperable with it, although it isn't yet becaus

null 3 Aug 2, 2022
Mateversum is a peer-to-peer WebXR metaverse project.

Mateversum ?? Mateversum (pronounced: MAH-tay-ver-sum) is a peer-to-peer WebXR metaverse project. The idea is that you'd be able to connect to a netwo

Ashley 23 Dec 21, 2022
Quick Peer-To-Peer UDP file transfer

qft QFT is a small application for Quick (and really reliable) Peer-To-Peer UDP file transfer. If a friend sent you here... ...look at the "Releases"

Daniel H. 99 Jan 7, 2023
Core library for Lightning Network peer-to-peer nostr platform

Mostro server This document explains how Mostro works. Overview Due to the growing need to be able to operate with Bitcoin without giving up personal

Mostro 16 Jan 4, 2023
A multiplayer web based roguelike built on Rust and WebRTC

Gorgon A multiplayer web-based roguelike build on Rust and WebRTC. License This project is licensed under either of Apache License, Version 2.0, (LICE

RICHΛRD ΛNΛYΛ 2 Sep 19, 2022
A pure Rust implementation of WebRTC API

A pure Rust implementation of WebRTC API

WebRTC.rs 2.7k Jan 7, 2023