Introduction
This is a simple peer-to-peer library for Rust + WASM, built on top of WebRTC. In the following example, we will connect to another peer and send it Hello world:
use ;
async
Installation
Usage
Setup
To establish a peer-to-peer connection, we need to send information like our IP address to the other peer so that it knows how to reach us. The server that we use to exchange this information is called a signaling server.
To initialize the P2P client, you need to pass the URL of the signaling server:
let mut p2p = P2Pnew.await;
In the previous example, we used wss://signaling.luisherasme.com as the signaling server. This server is free, and the code is open source so that you can create your own version. The code is available here.
Peer ID
The signaling server assigns a random, unique ID to each peer:
let id = p2p.id;
Connections
You can start a connection by calling p2p.connect with the peer ID of the destination peer.
let connection = p2p.connect.await;
You can get all the new connections by calling p2p.receive_connections:
let connections = p2p.receive_connections;
Receive meesages
To receive messages from the other peers that are connected to you, you can call the receive method:
let messages = connection.receive;
Send message
To send a message to another peer you have to use the send method:
let data = "EXAMPLE DATA YOU CAN SEND ANY &STR";
peer.send;
Custom ICE Servers
You can set your own ICE servers:
use ;
async
Furthermore, you can create an ICE server from a &str:
use ;
async
Examples
- P2P chat. Demo, Repository.