Loading...
const pc = new RTCPeerConnection();
pc.addEventListener('track', (e) => (video.srcObject = e.streams[0]));const pc = new RTCPeerConnection({
iceServers: [
{ urls: 'stun:stun.example.com:3478' },
{ urls: 'turn:turn.example.com:3478', username: 'user', credential: 'secret' }
]
});
pc.addEventListener('track', (e) => (video.srcObject = e.streams[0]));Without STUN and TURN servers, peers behind NAT or strict firewalls often fail to connect.
iceServers option lists STUN and TURN servers.icecandidate, track and connectionstatechange.close() is called when the call ends.Yes, for signaling. Peers exchange offers, answers and ICE candidates through your own channel, such as a WebSocket, before they connect directly.
STUN tells a peer its public address so it can try a direct connection. TURN relays the traffic when a direct connection is impossible.
Mobile networks often use NAT that blocks direct connections. A TURN server lets the traffic be relayed instead.