WebRTC (Web Real-Time Communication) is a real-time communication in the Web. This technology enables making calls, making video calls, conferences or transferring files from a browser. It was created as an answer to the growing demand for smooth communication. This technology was used in js-libp2p library as a solution for browsers to communicate with p2p network.
To create an initial connection between p2p network users, a special initial server is required to exchange information needed to establish a connection between the users. With some exceptions, further communication no longer takes place with the relay server.
You don't need much to run the WebRTC server, just VPS for $1 per month.
Reviews of super cheap VPS servers can be found at
On the docker-compose
Below you will find a simple script that automates the setting of needed programs on a clean machine. It is important to set the server's IP to a specific domain before running the docker-compose up, otherwise letsencrypt will not give us an ssl certificate.
I connect to the address
wrtc-star
└── docker-compose.yml
DOMAIN=wrtc-star1.chicago.nolife.cyou
scp -r wrtc-star root@$DOMAIN:
ssh root@$DOMAIN << EOF
set -xe
apt update
apt -yy install docker.io docker-compose
cd ~/wrtc-star
DOMAIN=$DOMAIN docker-compose up -d
EOF
The script copies the folder wrtc-star
docker-compose.yml
wrtc-star
docker-compose up -d
Example configuration needed to use a webrtc server in js-libp2p.
const p2p = await Libp2p.create({
addresses: {
listen: [
"/dns4/wrtc-star1.chicago.nolife.cyou/tcp/443/wss/p2p-webrtc-star/",
],
},
modules: {
transport: [Websockets, WebRTCStar],
connEncryption: [NOISE],
streamMuxer: [Mplex],
dht: KadDHT,
pubsub: Gossipsub,
},
config: {
peerDiscovery: {
autoDial: true,
dht: {
enabled: true,
},
webRTCStar: {
enabled: true,
},
},
pubsub: {
enabled: true,
},
},
});
As it turned out later, apart from the WebRTC Star server, you also need a TURN server for all users behind NAT (unfortunately js-libp2p has no nicely described how to do it according to p2p circuit/relay). In the listenerOptions
iceServers
RTCPeerConnection
libp2p-webrtc-peer
import Peer from "libp2p-webrtc-peer";
Peer.config = {
iceServers: [
{
urls: [
"stun:stun.l.google.com:19302",
"stun:global.stun.twilio.com:3478",
],
},
{
urls: "turn:turn1.chicago.nolife.cyou",
username: "any",
credential: "any",
},
],
sdpSemantics: 'unified-plan'
};
To create a TURN server we will use an open-source
turn
├── docker-compose.yml
└── turnserver.conf
docker-compose.yml
version: "3.3"
services:
turn:
image: instrumentisto/coturn
network_mode: "host"
volumes:
- ./turnserver.conf:/etc/turnserver.conf
Coturn configuration:
turnserver.conf
fingerprint
user=any:any
lt-cred-mech
realm=nolife.cyou
log-file=/var/log/turnserver/turnserver.log
simple-log
In the same way as with the server webrtc-star below I put a simple script to automate the setting of needed programs on a clean machine.
DOMAIN=turn1.chicago.nolife.cyou
scp -r turn root@$DOMAIN:
ssh root@$DOMAIN << EOF
set -xe
apt -yy install docker.io docker-compose
cd ~/turn
docker-compose up -d
EOF
Finally, it is best to check the connectivity of our server using
There should be at least one relay