WebRTC yêu cầu giao tiếp peer-to-peer giữa các trình duyệt. Cơ chế này cần signaling, thông tin mạng, điều khiển phiên và thông tin media. Nhà phát triển web có thể chọn cơ chế khác nhau như SIP, XMPP hoặc bất kỳ giao tiếp hai chiều nào. Ví dụ createSignalingChannel():
var signalingChannel = createSignalingChannel();
var pc;
var configuration = ...;
// chạy start(true) để khởi tạo cuộc gọi
function start(isCaller) {
pc = new RTCPeerConnection(configuration);
// gửi ice candidate cho peer kia
pc.onicecandidate = function(evt) {
signalingChannel.send(JSON.stringify({"candidate": evt.candidate}));
};
// khi remote stream đến, hiển thị trong video element
pc.onaddstream = function(evt) {
remoteView.src = URL.createObjectURL(evt.stream);
};
// lấy local stream, hiển thị và gửi
navigator.getUserMedia({"audio": true, "video": true}, function(stream) {
selfView.src = URL.createObjectURL(stream);
pc.addStream(stream);
if (isCaller)
pc.createOffer(gotDescription);
else
pc.createAnswer(pc.remoteDescription, gotDescription);
function gotDescription(desc) {
pc.setLocalDescription(desc);
signalingChannel.send(JSON.stringify({"sdp": desc}));
}
});
}
signalingChannel.onmessage = function(evt) {
if (!pc) start(false);
var signal = JSON.parse(evt.data);
if (signal.sdp)
pc.setRemoteDescription(new RTCSessionDescription(signal.sdp));
else
pc.addIceCandidate(new RTCIceCandidate(signal.candidate));
};