# WebRTC

> The WebRTC (Web Real-Time Communication) is a set of browser APIs and protocols for peer-to-peer audio, video and data transfer.

## What is WebRTC and how does it work?

The set of browser APIs and protocols for sending audio, video and arbitrary data directly between devices with very low latency. Used for video calls, screen sharing and voice agents. Browsers handle codecs, encryption and network traversal, but a signaling server is still needed.

- Topic: Real-Time Communication
- Type: technology

## Example

Wrong:

```js
const pc = new RTCPeerConnection({
  iceServers: [{ urls: 'stun:stun.example.com:3478' }],
});
```

Right:

```js
const pc = new RTCPeerConnection({
  iceServers: [
    { urls: 'stun:stun.example.com:3478' },
    {
      urls: 'turns:turn.example.com:443?transport=tcp',
      username: creds.username,
      credential: creds.credential,
    },
  ],
});
```

A TURN server with short-lived credentials from your backend lets calls connect even behind strict firewalls.

## Common mistakes

- Expecting WebRTC to connect peers without any server at all.
- Testing only on one network and missing NAT traversal failures.

## Key points

- A signaling channel exists to exchange the offer, answer and ICE candidates between peers.
- A TURN server is configured, not only STUN, so calls work behind strict firewalls.
- Media tracks are stopped and the peer connection is closed when the call ends.

## Interview questions

### Is WebRTC encrypted?

Yes. Encryption is mandatory: media uses SRTP and data channels use DTLS, so unencrypted WebRTC is not possible.

### WebRTC vs WebSocket: what is the difference?

WebSocket connects a client to a server over TCP. WebRTC connects devices directly, mostly over UDP, and handles audio, video and data.

### Can WebRTC send files or text, not just video?

Yes. `RTCDataChannel` sends arbitrary data between peers, with reliable or unreliable delivery.

## Related cards

- [RTCPeerConnection](https://wonderium.dev/js/rtc-peer-connection.md)
- [Signaling](https://wonderium.dev/web/signaling.md)
- [ICE](https://wonderium.dev/web/ice.md)
- [TURN](https://wonderium.dev/web/turn.md)
- [SFU](https://wonderium.dev/web/sfu.md)

---

- Level: [Web Platform](https://wonderium.dev/web.md)
- HTML version: https://wonderium.dev/web/webrtc
- Last updated: 2026-11-25
