This document provides a comprehensive explanation of the steps involved in establishing a peer-to-peer (P2P) connection using WebRTC. WebRTC enables real-time communication capabilities, such as audio, video, and data transfer, directly between browsers without requiring additional plugins.
Establishing a WebRTC connection involves two major phases:
- Signaling Process: Exchange of metadata (offer, answer, and ICE candidates) through a signaling server.
- Media Streaming: Sending and receiving media (audio, video) or data streams between peers.
Below are the detailed steps for each phase.
The signaling process involves the exchange of Session Description Protocol (SDP) and ICE candidates to set up a P2P connection. Here’s how it works:
-
Create an
RTCPeerConnection
:- Create a new instance of
RTCPeerConnection
to handle the P2P connection. - Example:
const pc = new RTCPeerConnection(configuration);
- Create a new instance of
-
Create an Offer:
- Generate an offer SDP using
createOffer()
.const offer = await pc.createOffer();
- Generate an offer SDP using
-
Set the Local Description:
- Assign the generated offer SDP to the local description.
await pc.setLocalDescription(offer);
- Assign the generated offer SDP to the local description.
-
Send the Offer:
- Transmit the offer to the other browser (Browser 2) via a signaling server.
-
Receive the Offer:
- Obtain the offer from the signaling server.
-
Set the Remote Description:
- Assign the received offer SDP to the remote description.
await pc.setRemoteDescription(offer);
- Assign the received offer SDP to the remote description.
-
Create an Answer:
- Generate an answer SDP using
createAnswer()
.const answer = await pc.createAnswer();
- Generate an answer SDP using
-
Set the Local Description:
- Assign the generated answer SDP to the local description.
await pc.setLocalDescription(answer);
- Assign the generated answer SDP to the local description.
-
Send the Answer:
- Transmit the answer back to Browser 1 through the signaling server.
-
Receive the Answer:
- Obtain the answer from the signaling server.
-
Set the Remote Description:
- Assign the received answer SDP to the remote description.
await pc.setRemoteDescription(answer);
- Assign the received answer SDP to the remote description.
At this stage, the signaling process is complete, and the WebRTC connection is established.
Once the P2P connection is established, you can exchange media streams (audio/video) between peers. Below are the steps:
-
Request Camera and Microphone Permissions:
- Use
navigator.mediaDevices.getUserMedia()
to request access to audio and video streams.const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
- Use
-
Add Media Tracks to the Peer Connection:
- Add the acquired media tracks to the
RTCPeerConnection
.stream.getTracks().forEach((track) => { pc.addTrack(track, stream); });
- Add the acquired media tracks to the
-
Handle Incoming Media Tracks:
- Listen for the
ontrack
event to handle media tracks sent by the remote peer.pc.ontrack = (event) => { const remoteStream = new MediaStream(); event.streams[0].getTracks().forEach((track) => { remoteStream.addTrack(track); }); // Assign the remote stream to a video element remoteVideoElement.srcObject = remoteStream; };
- Listen for the
At this point, both peers can send and receive media streams, enabling real-time communication.
- RTCPeerConnection: Handles the connection and media transfer between peers.
- Session Description Protocol (SDP): Contains metadata about the connection (e.g., codecs, formats).
- Interactive Connectivity Establishment (ICE): Manages NAT traversal and network connectivity.
- Signaling Server: A server responsible for exchanging SDP and ICE candidates (not part of WebRTC itself).
Here is a simple example of a signaling server:
const io = require("socket.io")(3000);
io.on("connection", (socket) => {
socket.on("offer", (data) => {
socket.to(data.target).emit("offer", data);
});
socket.on("answer", (data) => {
socket.to(data.target).emit("answer", data);
});
socket.on("ice-candidate", (data) => {
socket.to(data.target).emit("ice-candidate", data);
});
});
By following the above steps, you can successfully establish a WebRTC connection and exchange media streams between two peers.