Home

 / Blog / 

RTCPeerConnection - Everything You Need To Know

RTCPeerConnection - Everything You Need To Know

November 20, 20234 min read

Share

RTCPeerConnection | Cover Image.png

Definition

RTCPeerConnection is a core component of the WebRTC (Web Real-Time Communication) API used for establishing direct peer-to-peer communication between web browsers or devices. It allows the exchange of audio, video, and arbitrary data directly between clients without the need for an intermediary server.

What does it really mean?

Think of WebRTC and its components like a private, efficient office. WebRTC is the office building itself, providing the space and tools for communication. RTCPeerConnection acts like an internal phone system, allowing direct calls between employees without an external network. MediaStream is akin to video and audio equipment, used for capturing and sharing presentations. Lastly, RTCDataChannel is like the office's internal mail, letting employees send data directly to each other. This setup ensures that all communication and data exchange is quick, direct, and secure, much like WebRTC enables seamless, real-time interactions on the web.

History of RTCPeerConnection

What was used before RTCPeerConnection

Before the advent of RTCPeerConnection and WebRTC, real-time communication on the web was more complex and less efficient. The earlier methods included:

  1. Flash-based Solutions: Many websites used Adobe Flash to enable audio and video communication. However, Flash had limitations in terms of performance, security, and compatibility with mobile devices.
  2. Plugins and Third-party Software: Users often had to download and install specific plugins or software to enable real-time communication. This approach lacked standardization and could pose security risks.
  3. Server-based Communication: Real-time data was frequently relayed through central servers rather than being directly exchanged between peers. This method resulted in higher latency and increased server load.

What challenges did it face?

  1. Limited Cross-Platform Compatibility: Technologies like Adobe Flash or proprietary plugins were not uniformly supported across different browsers and devices, especially mobile platforms.
  2. Performance Issues: These older methods often struggled with higher latency and less efficient streaming, impacting the quality of real-time communication.
  3. Security Risks: Plugins and third-party software pose potential security vulnerabilities, making them less favorable for secure communications.
  4. User Experience: Requiring users to install plugins or additional software created barriers to entry, negatively affecting the user experience.
  5. Scalability and Cost: Server-based communication methods were less scalable and more costly, as they required significant server resources to relay data between users.

How does RTCPeerConnection solve them?

RTCPeerConnection addressed the challenges of older technologies in several ways:

  1. Cross-Browser Compatibility: It is a standardized API, supported by most modern web browsers, enabling consistent functionality across different platforms, including mobile devices.
  2. Direct Peer-to-Peer Communication: By allowing direct data exchange between peers without going through a server, it significantly reduces latency and improves performance.
  3. No Need for Plugins: Being a native part of the browser, RTCPeerConnection eliminates the need for users to install third-party plugins or software.
  4. Enhanced Security: It mandates end-to-end encryption for all data streams, improving the security of communications.
  5. Scalability and Lower Costs: Direct peer-to-peer communication reduces reliance on servers, leading to better scalability and lower operational costs.

How does RTCPeerConnection work?

  1. Initialization: A new RTCPeerConnection object is created in JavaScript. This object is configured with ICE servers (STUN and TURN), which help in discovering the public IP address and managing NAT traversal.
  2. Media Stream Acquisition: Using the getUserMedia API, media streams (audio and video) are acquired from the user's device and added to the RTCPeerConnection.
  3. Creating Offer/Answer: One peer creates an offer using createOffer() method and sets it as its local description. This offer includes information about the media formats and codecs supported, as well as ICE candidates (potential connection pathways).
  4. Signaling Process: The offer is then sent to the other peer using a signaling server (a separate server process that isn't part of WebRTC). This server doesn’t process the offer but simply forwards it.
  5. Remote Description and Answer: The receiving peer sets this offer as its remote description and creates an answer using createAnswer(), which is then set as its local description and sent back to the initiator.
  6. ICE Candidate Exchange: Both peers exchange ICE candidates through the signaling server. onicecandidate event handler detects and sends new ICE candidates.
  7. Connection Establishment: Once both peers have exchanged offers, answers, and ICE candidates, the RTCPeerConnection establishes a connection. The best suitable ICE candidates (network paths) are chosen for the connection.
  8. Data Exchange: The established connection can now exchange media streams and/or data directly between peers.
  9. Connection Monitoring and Maintenance: The connection is monitored for changes in network conditions, and ICE candidates can be renegotiated if needed to maintain the connection.

How RTCPeerConnection Works

What is the relationship between RTCPeerConnection and Event handlers?

The relationship between RTCPeerConnection and event handlers is that of a system and its interactive components. RTCPeerConnection generates various events during its lifecycle, such as changes in the connection state, receiving new media streams, or encountering errors. Event handlers are functions that are assigned to respond to these events. When an event occurs, the corresponding event handler is executed, allowing the application to react appropriately. For instance, the onicecandidate event handler is triggered when a new ICE candidate is found, enabling the application to send this candidate to the remote peer via the signaling mechanism. This relationship is crucial for managing the dynamic, real-time aspects of peer connections in web applications.

Key Event Handlers:

  1. onicecandidate: Triggered when the local ICE agent needs to deliver a message to the other peer through the signaling server.
  2. ontrack: Activated when a new media track is received from the remote peer.
  3. onconnectionstatechange: Fires when the connection state of the RTCPeerConnection changes.
  4. ondatachannel: Occurs when a data channel is signaled by the remote peer as open.
  5. onnegotiationneeded: Triggered when the local side of the connection needs to begin a new negotiation of session parameters.
  6. oniceconnectionstatechange: Happens when the ICE connection state changes, indicating the status of the ICE gathering process between peers.
  7. onicegatheringstatechange: Occurs when the state of the ICE gathering process changes.

Frequently Asked Questions

How do I connect to peer-to-peer on WebRTC?

To connect peer-to-peer on WebRTC:

  1. Create RTCPeerConnection objects on both peers.
  2. Exchange offer/answer and ICE candidates between peers using a signaling server.
  3. Once the connection is established, use the connection for media/data exchange.

Does peer-to-peer use IP?

Yes, peer-to-peer communication in WebRTC, including RTCPeerConnection, uses IP addresses. ICE candidates, exchanged during the connection setup, contain IP address information to establish the direct link.

References

  • https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection
  • https://webrtc.org/getting-started/peer-connections-advanced
  • https://webrtc.org/getting-started/peer-connections
  • https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection
  • https://webrtc.github.io/samples/src/content/peerconnection/pc1/

Glossary

Share

Related articles

See all articles