PeerConnection (RTCPeerConnection) is the core JavaScript API in WebRTC. It represents a connection between the local device and a remote peer, handling the full lifecycle of a WebRTC session including SDP negotiation, ICE NAT traversal, and DTLS/SRTP encrypted media exchange.
PeerConnection lifecycle
- Creation:
new RTCPeerConnection(config)– configuration includes STUN/TURN server URLs - Adding media:
addTrack()oraddTransceiver()to attach audio/video tracks - SDP negotiation: The JSEP offer/answer exchange via
createOffer(),setLocalDescription(),setRemoteDescription(),createAnswer() - ICE connectivity: Candidates are gathered and exchanged, connectivity checks run
- Connected: Media flows between peers via encrypted SRTP
- Monitoring:
getStats()provides real-time quality metrics - Closure:
close()terminates the session
Key features
- Unified Plan SDP: Each media track gets its own m= line in the SDP
- Trickle ICE: ICE candidates are sent incrementally for faster connection setup
- Data Channel: Created via
createDataChannel()on the PeerConnection - BUNDLE: All media multiplexed over a single transport connection
PeerConnection is defined by the W3C WebRTC specification and implemented in all modern browsers via libWebRTC.


