For most WebRTC applications to function a server is required for relaying the traffic between peers, since a direct socket is often not possible between the clients (unless they reside on the same local network). The common way to solve this is by using a TURN server. The term stands for Traversal Using Relays around NAT, and it is a protocol for relaying network traffic.
There are currently several options for TURN servers available online, both as self-hosted applications (like the open-source COTURN project) and as cloud provided services.
Once you have a TURN server available online, all you need is the correct
RTCConfiguration
for your client application to use it. The following code
snippet illustrates a sample configuration for a RTCPeerConnection
where the
TURN server has the hostname my-turn-server.mycompany.com
and is running on
port 19403
. The configuration object also support the username
and
credential
properties for securing the access to the server. These are
required when connecting to a TURN server.
const iceConfiguration = {
iceServers: [
{
urls: 'turn:my-turn-server.mycompany.com:19403',
username: 'optional-username',
credential: 'auth-token'
}
]
}
const peerConnection = new RTCPeerConnection(iceConfiguration);