Fireship · @fireship

WebRTC in 100 Seconds // Build a Video Chat app from Scratch

Uploaded March 15, 2021 · Length 11:19

WebRTC in 100 Seconds // Build a Video Chat app from Scratch

Summary

Build a peer-to-peer video chat app from scratch using WebRTC, vanilla JavaScript, and Firebase as the signaling server.

  • webrtc
  • video chat
  • peer-to-peer
  • javascript
  • firebase
  • signaling server
  • web development

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    webrtc exchange real-time audio video

  2. 0:03

    streams with your friends

  3. 0:04

    entirely in the browser i'm here live

  4. 0:06

    that's not i'm not a cat

  5. 0:08

    if you want to build your own video

  6. 0:09

    conferencing app to capitalize on the

  7. 0:11

    work from home boom

  8. 0:12

    then webrtc is the api you're looking

  9. 0:14

    for because it allows you to establish a

  10. 0:17

    peer-to-peer connection between two or

  11. 0:18

    more browsers

  12. 0:19

    where they can exchange audio video

  13. 0:21

    media directly without the need for a

  14. 0:23

    third-party server or native app

  15. 0:25

    it works like this the first pier will

  16. 0:27

    create an offer

  17. 0:28

    asking for another peer to connect to

  18. 0:30

    them this will result

  19. 0:31

    in an stp object or session description

  20. 0:34

    protocol

  21. 0:34

    which contains information describing

  22. 0:36

    the peer-to-peer connection

  23. 0:38

    like the video codec timing and so on

  24. 0:40

    the data will then be saved in a server

  25. 0:42

    where it can then be read by another

  26. 0:44

    peer to answer the call which is

  27. 0:45

    achieved by creating an stp answer

  28. 0:48

    and writing that to the server this

  29. 0:50

    process is known as

  30. 0:51

    signaling and it's handled by a third

  31. 0:53

    party server the signaling server allows

  32. 0:55

    the two parties to securely exchange

  33. 0:57

    connection data

  34. 0:58

    but never touches the media that's

  35. 0:59

    actually transmitted between the peers

  36. 1:01

    but here's where things get tricky most

  37. 1:03

    devices in the real world sit behind

  38. 1:05

    firewalls

  39. 1:05

    and ip addresses constantly change

  40. 1:07

    thanks to network address translation

  41. 1:10

    this makes peer-to-peer connection

  42. 1:11

    complicated from a networking standpoint

  43. 1:13

    but luckily there's a standard called

  44. 1:15

    interactive connectivity establishment

  45. 1:17

    or ice which helps clients coordinate

  46. 1:20

    the discovery of their public-facing ip

  47. 1:22

    addresses

  48. 1:23

    now both peers will generate a list of

  49. 1:25

    ice candidates

  50. 1:26

    which contain an ip address and port

  51. 1:28

    that pier 1 can use to connect to pier

  52. 1:31

    2.

  53. 1:31

    in the background webrtc will do this by

  54. 1:34

    making a series of requests to a stun

  55. 1:36

    server

  56. 1:36

    a stun server is not something you need

  57. 1:38

    to set up on your own because there are

  58. 1:40

    many free options out there

  59. 1:41

    from reliable sources like google each

  60. 1:43

    peer will save their ice candidates in

  61. 1:45

    the database where they can then be read

  62. 1:47

    by the other peer

  63. 1:48

    the algorithm will then automatically

  64. 1:50

    determine which candidate is best

  65. 1:52

    at which point real-time media can begin

  66. 1:54

    flowing between the two peers

  67. 1:55

    this has been webrtc in 100 seconds if

  68. 1:58

    you want to see more short videos like