PeerConnection

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

  1. Creation: new RTCPeerConnection(config) - configuration includes STUN/TURN server URLs
  2. Adding media: addTrack() or addTransceiver() to attach audio/video tracks
  3. SDP negotiation: The JSEP offer/answer exchange via createOffer(), setLocalDescription(), setRemoteDescription(), createAnswer()
  4. ICE connectivity: Candidates are gathered and exchanged, connectivity checks run
  5. Connected: Media flows between peers via encrypted SRTP
  6. Monitoring: getStats() provides real-time quality metrics
  7. 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.

Additional reading

Tsahi Levent-Levi

Tsahi Levent-Levi

Independent WebRTC analyst. 20+ years in telecom, 13 focused on WebRTC. Writes for developers and product teams who need to understand, not just implement, real-time communications.