What happens when you type a *video chat* URL in your browser?
Tags: webdev, video, webrtc, daily
Published: 2020-08-13 18:56:03

#Backstory 
Last month I joined the team at [Daily](https://www.daily.co/). Daily builds video chat APIs that simplify adding video calls to any website or app. I had to get to know their APIs during [the interview process](https://www.notion.so/dailyco/How-we-hire-at-Daily-co-5b73de95db894f9c926f61d86da85ba6), and, sure enough, it took me just a few minutes to get a call embedded on my website. 

That was just for the take home project, though. When prepping for any engineering interview, I practice answering the infamous question, “What happens when you type `company-i-am-interviewing-at.com` in your browser?” I practice because I’ve messed up the answer more than once. 

There are many ways to answer this question, and there are lots of great resources out there (Even other [Dev.to](https://dev.to/deepika_banoth/what-happens-when-i-type-a-url-in-browser-3i5o) posts!). 

But, visiting a Daily URL felt like a different experience. After I [signed up for an account and generated my first room link](https://dashboard.daily.co/signup), when I pasted it in my browser, I went directly to a video call. I realized that I actually didn’t know: 

* What happens when you type a video chat URL in your browser? 
* How do call participants trade audio and video? 
* When Meryl Streep, Christine Baranski and Audra McDonald [serenaded Stephen Sondheim for his 90th birthday](https://variety.com/2020/music/news/stephen-sondheim-birthday-celebration-jake-gyllenhaal-meryl-streep-1234590377/), how did that actually work? 

![Singers toast Sondheim on video chat](https://dev-to-uploads.s3.amazonaws.com/i/n7180qo1xc367d8bu6qu.jpg)

Since most of us are spending a lot more time on video calls in 2020, I became especially curious about these questions.

This post walks through what I’ve learned so far, at a high-level. It covers peer-to-peer (P2P) video chats, calls when different participants’ browsers talk directly to each other. I won’t be getting into other kinds of video calls, getting into the weeds about how connecting to the internet works, or covering many other things that I don’t know that I don’t know yet. Instead, like the answers to most questions, this is a springboard for more things to ask later. With that, let’s jump in: 

![Cartoon cannonballs into pool](https://media.giphy.com/media/9P8QICQMK2uoTxUctG/giphy.gif) 

#URLs: a quick refresh
URL stands for Uniform Resource Locator. A URL points us to the server, represented by the domain name, where the resource we’re looking for lives. 

You typically see the letters HTTP before a URL. That’s the language the browser communicates in: Hypertext Transfer Protocol. The browser sends an HTTP request asking for the resource specified in the URL.  

The request starts the Domain Name System (DNS) lookup process. 

![Wile Coyote looks around trying to find something](https://media.giphy.com/media/26n6WywJyh39n1pBu/giphy.gif)

That process alone deserves many [a standalone post](https://dev.to/chrisachard/dns-record-crash-course-for-web-developers-35hn). For our purposes, we’ll fast forward to the part when a resource is found. Once the browser knows the location, it initiates a TCP connection with the server hosting the resource, and the information exchange begins. 

So, at the highest level, a browser says, “Hi! Can I have this resource?” After a negotiation process, a server responds with, “Sure, here you go!” [0]. 

#Daily URLs 
Unlike a static web page (like the one you’re reading right now!), realtime moving images and audio are traded back and forth during a video call. Head to any Daily room URL, and two things will happen: 1) You’ll see a video chat interface and 2) Your browser will likely ask for permission to use your video and microphone. [Sign up for a Daily account](https://dashboard.daily.co/signup) and create your own room URL to see for yourself. 

How do those two things happen? 

Again, a URL points to a resource. We can think of a resource as an html site. A Daily site runs some extra scripts that get in touch with a signaling server.

![A computer connects to a signaling cloud via HTTPS](https://dev-to-uploads.s3.amazonaws.com/i/szdpnu5ylfpwhb2czjwg.png)

If you’ve ever used an online dating app or found an apartment on a site like Craigslist, you’re more familiar with how signaling servers work than you think. On Craigslist, you might see a post about a new apartment, including information like the neighborhood, rent, and how to reply to the poster. After you reply, you often wind up communicating directly with the person who made the post, without going through Craigslist at all. It’s similar in dating apps. 

![David Rose from Schitt’s Creek offers to talk any time](https://media.giphy.com/media/cJYR7daLY7F73jy2b4/giphy.gif)

It's also like what happens in peer-to-peer (P2P) video calls. Once I’ve typed a URL in my browser and the server lets me into a chat, the server also hands me the IP addresses of the other people on the call. When I have their addresses, I know where to send out my audio and video streams (to each of their IPs), and can receive their audio/video back as well. If someone else joins the call after me, the server lets me and the others know, and we start sending/receiving media directly with the new caller. 

We know _how_ to do all of this because our browsers all speak the same language: WebRTC. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yh0g2f8ie2ckn0lppy5b.jpg)

#WebRTC what? 
[WebRTC](https://webrtc.org/) is the internet’s open, secure standard for exchanging video, voice, and generic data between browsers. [Daily](https://www.daily.co/), and many other video chat applications, is built on top of it. WebRTC transmits data over [UDP (User Datagram Protocol)](https://www.cloudflare.com/learning/ddos/glossary/user-datagram-protocol-udp/), which is encrypted via [DTLS (Datagram Transport Layer Security)](https://webrtcglossary.com/dtls/).


WebRTC gives us the [MediaDevices API](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices). The MediaDevices API is the browser’s API for, well, accessing and working with our media devices, like cameras and mics. We can see this API in action by running `navigator.mediaDevices.getUserMedia()` in our console. Before we do, let's walk through each part of that command: 

* [navigator](https://www.w3schools.com/jsref/obj_navigator.asp): holds information about our browser. 
* [.mediaDevices](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices): again, this is the browser’s API for working with our cameras, mics, and other devices. 
* [.getUserMedia()](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia): the method that prompts the user to turn on their camera or microphone. We pass an Object to this method that specifies whether or not we need the video and audio streams.

Open your browser console on any website, and paste in the below gist to play with this. You should be prompted to allow camera access. When you grant permission, your camera light should turn on: 

{% gist https://gist.github.com/kimberleejohnson/5efc98cf95ce88a3afec0b845fa12663 %}

If, like me, you’ve got a green camera light, at this point you might be reminded of a certain Great Gatsby, looking out at a green light.

![Leonard DiCaprio points to green light](https://media.giphy.com/media/Yt6bynq3Im19u/giphy.gif) 

Unlike Gatsby, though, your dreams, or, at least your ability to share your video stream, are within reach [1]! 

So far, we know that when we type a video chat URL into our browser, we connect to a signaling server to get the IP addresses and local streams of everybody else on the call. We know that the browser accesses our own camera via WebRTC’s MediaDevices API. What about sending our own stream out to the other callers, so they can see it? 

There’s a WebRTC API for that, too. 

We can attach our camera stream to another caller’s connection through the [RTCPeerConnection API](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection). The other callers then add our stream to a [MediaStream object](https://developer.mozilla.org/en-US/docs/Web/API/MediaStream). We do the same thing when they send a stream our way [2]. All of this data transmission happens over the User Datagram Protocol [(UDP)](https://www.cloudflare.com/learning/ddos/glossary/user-datagram-protocol-udp/). 

Of course, for these streams to be displayed, they need to be placed inside HTML elements. You can see examples of how that could be done in the [WebRTC documentation](https://webrtc.org/getting-started/overview) or in [Daily demos](https://github.com/daily-demos). 

#Putting it all together 
So, what happens when you type a video chat URL in your browser? 

At the highest level, your browser finds a resource that asks a signaling server if you can join the call. The server remembers details about the call, like who else is in the chat and where to find them (their IP addresses). When your browser connects to the server and you're let into the call, your browser shares your video and audio streams directly with other participants, and receives them back, through WebRTC APIs. 

#What’s next? 
I mentioned this way back in the intro, but we’ve only covered how chat works using WebRTC and peer-to-peer (P2P) calls, when browsers talk directly to each other. There’s a whole other potential ~~acronym~~ architecture to explore: [Selective Forwarding Unit](https://webrtcglossary.com/sfu/). 

That’s not to mention a bunch of other questions, like, how does muting your mic or sharing your screen work? What about limiting who can join a call? I’m excited to learn the answers to these and then some at Daily. Follow along here on [Dev.to](https://dev.to/trydaily), over on the [Daily blog](https://www.daily.co/blog), or on [GitHub](https://github.com/daily-co). 

If you’d like to pair on embedding video chat into your own app or website, or want to chat all things video APIs, give me a shout on Twitter [@kimeejohnson](https://twitter.com/kimeejohnson).

![Cartoon waves on video chat](https://media.giphy.com/media/XHM2W1ZBo3DJMDwRM2/giphy.gif) 

### Footnotes and resources 
[0] You also often see an S after HTTP, but getting into HTTPS is a bit beyond the scope of this post. Quickly: the ‘S’ stands for secure, meaning the browser and server will negotiate a key to encrypt (and decrypt) the information they exchange (Check out  [Julia Evans’ https breakdown](https://twitter.com/b0rk/status/1159812119099060224) for a visual).

[1] If your camera light is a color other than green, insert your own art deco or yearning joke here. 

[2] WebRTC’s docs go into [details and code samples](https://webrtc.org/getting-started/remote-streams) 

Resources: 
* [WebRTC documentation](https://webrtc.org/)
* [High Performance Browser Networking, O’Reilly](https://hpbn.co/webrtc/)
* [Daily blog](https://www.daily.co/blog/peer-to-peer-for-the-win-designing-daily-co-p2p-video-calls)
* Cover photo by way of [Smash Illustrations](https://usesmash.com/)