Firebase + WebRTC Codelab
solution
Full code solution can be found under the branch:This is the GitHub repo for the FirebaseRTC codelab. This will teach you how to use Firebase Cloud Firestore for signalling in a WebRTC video chat application. The solution to this codelab can be seen in the solution branch.
See http://webrtc.org for details.
Note: the following instructions were copied and updated/corrected from https://webrtc.org/getting-started/firebase-rtc-codelab.
Introduction
In this codelab, you'll learn how to build a simple video chat application using the WebRTC API in your browser and Cloud Firestore for signaling. The application is called FirebaseRTC and works as a simple example that will teach you the basics of building WebRTC enabled applications.
Note: Another option for signaling could be Firebase Cloud Messaging. However, that is currently only supported in Chrome and in this codelab we will focus on a solution that works across all browsers supporting WebRTC.
What you'll learn
- Initiating a video call in a web application using WebRTC
- Signaling to the remote party using Cloud Firestore
What you'll need
Before starting this codelab, make sure that you've installed:
- npm which typically comes with Node.js - Node LTS is recommended
Instructions
-
Create and set up a Firebase project
- Create a Firebase project
- In the Firebase console, click Create a project, then name the Firebase project "FirebaseRTC".
- Remember the Project ID for your Firebase project.
Note: If you go to the home page of your project, you can see it in Settings > Project Settings (or look at the URL!)
- Disable Google Analytics
- Click Create project.
- Creation takes a moment. When your project is created, click Continue.
The application that you're going to build uses two Firebase services available on the web:
- Cloud Firestore
- to save structured data on the Cloud and get instant notification when the data is updated
- Firebase Hosting
- to host and serve your static assets
For this specific codelab, you've already configured Firebase Hosting in the project you'll be cloning. However, for Cloud Firestore, we'll walk you through the configuration and enabling of the services using the Firebase console.
- Create a Firebase project
-
Enable Cloud Firestore
The app uses Cloud Firestore to save the chat messages and receive new chat messages.
- In the Firebase sidebar, navigate to Build -> Cloud Firestore.
- Click Create database in the Cloud Firestore pane.
- Select the Start in test mode option, read the warning about database security, and click Next. Then click Enable after reading the warning about the security rules. Test mode ensures that you can freely write to the database during development.
-
Get the sample code
- On your local machine, clone the codelab GitHub repository from the command line:
git clone https://github.com/webrtc/FirebaseRTC
[DELETE in PR: for now, usegit clone https://github.com/meldaravaniel/FirebaseRTC
]- The sample code should have been cloned into the FirebaseRTC directory.
- Make sure your command line is run from this directory from now on by executing
cd FirebaseRTC
As you work through the tutorial, open the files in
FirebaseRTC
in your editor and change them according to the instructions below. This directory contains the starting code for the codelab which consists of a not-yet functional WebRTC app. We'll make it functional throughout this codelab. - On your local machine, clone the codelab GitHub repository from the command line:
-
Install the Firebase Command Line Interface
The Firebase Command Line Interface (CLI) allows you to serve your web app locally and deploy your web app to Firebase Hosting.
Note: To install the CLI, you need to install npm which typically comes with Node.js.
- Install the CLI by running the following npm command:
npm -g install firebase-tools
- On unix and doesn't work? You may need to run the command using sudo instead.
- Verify that the CLI has been installed correctly by running the following command:
firebase --version
- Make sure the version of the Firebase CLI is v6.7.1 or later.
- Authorize the Firebase CLI by running the following command:
firebase login
- if the terminal you use is 'non-interactive' (eg. git-bash) you may need to append
--interactive
to the above command.
- if the terminal you use is 'non-interactive' (eg. git-bash) you may need to append
You've set up the web app template to pull your app's configuration for Firebase Hosting from your app's local directory and files. But to do this, you need to associate your app with your Firebase project.
- Associate your app with your Firebase project by running the following command:
firebase use --add
- you may need to append
--interactive
to the above command. - When prompted, select your Project ID (from the Create Firestore Project step), then give your Firebase project an alias. An alias is useful if you have multiple environments (production, staging, etc). However, for this codelab, let's just use the alias of default.
- you may need to append
- Follow the remaining instructions in your command line.
- Install the CLI by running the following npm command:
-
Run the local server
You're ready to actually start work on our app! Let's run the app locally!
- Run the following Firebase CLI command:
firebase serve --only hosting
- you may need to append
--interactive
to the above command. - Your command line should display the following response:
hosting: Local server: http://localhost:5000
- We're using the Firebase Hosting emulator to serve our app locally. The web app should now be available from http://localhost:5000.
- you may need to append
- Open your app at http://localhost:5000 You should see your copy of FirebaseRTC which has been connected to your Firebase project. The app has automatically connected to your Firebase project.
Note: if you want to access the server from other devices on your local network, you need to add
-o 0.0.0.0
at the end of thefirebase serve
command. - Run the following Firebase CLI command:
-
Creating a new room
In this application, each video chat session is called a room. A user can create a new room by clicking a button in their application. This will generate an ID that the remote party can use to join the same room. The ID is used as the key in Cloud Firestore for each room.
Each room will contain the
RTCSessionDescriptions
for both the offer and the answer, as well as two separate collections with ICE candidates from each party.Your first task is to implement the missing code for creating a new room with the initial offer from the caller. Open public/app.js, find the comment
// Add code for creating a room below
, and add the following code. (You should add the code before the corresponding// Add code for creating a room above
; the same goes for the rest of the code blocks.)const offer = await peerConnection.createOffer(); await peerConnection.setLocalDescription(offer); console.log('Created offer:', offer); const roomWithOffer = { 'offer': { type: offer.type, sdp: offer.sdp } } await roomRef.set(roomWithOffer); roomId = roomRef.id; console.log(`New room created with SDP offer. Room ID: ${roomRef.id}`); document.querySelector('#currentRoom').innerText = `Current room is ${roomId} - You are the caller!`
The first line creates an
RTCSessionDescription
that will represent the offer from the caller. This is then set as the local description and, finally, written to the new room object in Cloud Firestore.Next, we will listen for changes to the database and detect when an answer from the callee has been added. Find the comment
// Listening for remote session description below
and add the following code:roomRef.onSnapshot(async snapshot => { const data = snapshot.data(); if (!peerConnection.currentRemoteDescription && data && data.answer) { console.log('Got remote description: ', data.answer); const rtcSessionDescription = new RTCSessionDescription(data.answer); await peerConnection.setRemoteDescription(rtcSessionDescription); } });
This will wait until the callee writes the
RTCSessionDescription
for the answer, and set that as the remote description on the callerRTCPeerConnection
. -
Joining a room
The next step is to implement the logic for joining an existing room. The user does this by clicking the "Join room" button and entering the ID for the room to join. Your task here is to implement the creation of the
RTCSessionDescription
for the answer and update the room in the database accordingly. Find the comment// Code for creating SDP answer below
in thejoinRoomById(roomId)
method and add the following code:const offer = roomSnapshot.data().offer; console.log('Got offer:', offer); await peerConnection.setRemoteDescription(new RTCSessionDescription(offer)); const answer = await peerConnection.createAnswer(); console.log('Created answer:', answer); await peerConnection.setLocalDescription(answer); const roomWithAnswer = { answer: { type: answer.type, sdp: answer.sdp, }, }; await roomRef.update(roomWithAnswer);
In the code above, we start by extracting the offer from the caller and creating a
RTCSessionDescription
that we set as the remote description. Next, we create the answer, set it as the local description, and update the database. The update of the database will trigger theonSnapshot
callback that we added in the previous step on the caller side. That callback will set the remote description based on the answer from the callee. This completes the exchange of theRTCSessionDescription
objects between the caller and the callee. -
Collect ICE candidates
Before the caller and callee can connect to each other, they also need to exchange ICE candidates that tell WebRTC how to connect to the remote peer. Your next task is to implement the code that listens for ICE candidates and adds them to a collection in the database. Find the comment
// collect ICE Candidates function below
and add the following function:async function collectIceCandidates(roomRef, peerConnection, localName, remoteName) { const candidatesCollection = roomRef.collection(localName); peerConnection.addEventListener('icecandidate', event => { if (event.candidate) { const json = event.candidate.toJSON(); candidatesCollection.add(json); } }); roomRef.collection(remoteName).onSnapshot(snapshot => { snapshot.docChanges().forEach(change => { if (change.type === "added") { const candidate = new RTCIceCandidate(change.doc.data()); peerConnection.addIceCandidate(candidate); } }); }) }
This function does two main things.
- collects ICE candidates from the WebRTC API and adds them to the database
- listens for added ICE candidates from the remote peer and adds them to its
RTCPeerConnection
instance.
It is important when listening to database changes to filter out anything that isn't a new addition, since we otherwise would have added the same set of ICE candidates over and over again.
Complete this step by uncommenting the calls to this function in both the
joinRoomById
andcreateRoom
methods; you can find them after// Uncomment to collect ICE candidates below
. -
Try it out
- Open a new browswer tab at http://localhost:5000, or refresh the tab you opened above.
- Mute your speakers to avoid loud piercing feedback!
- Click on Open camera & microphone; give permission to the app to use them, if requested.
- Click on Create room. The app will display the room ID.
- Open another browswer tab, click Open camera & microphone, and click Join room. Paste in the ID from the previous step. The two instances should connect.
To connect with a different device on your local network:
- Make sure that your start the server with
-o 0.0.0.0
. - Find the IP address of your server. Let's say it's
192.168.1.5
. - On your remote device, you need to enable the camera for "insecure orgins," since you are connecting to the server via http, not https. For Chrome, you can do this by navigating to
chrome://flags/#unsafely-treat-insecure-origin-as-secure
, and addinghttp://192.168.1.5:5000/
to the list of origins. - Connect as above, except that you will have to enter the room ID manually.
-
Conclusion
In this codelab you learned how to implement signaling for WebRTC using Cloud Firestore, as well as how to use that for creating a simple video chat application.
To learn more, visit the following resources: