React(ing) to WebRTC: Build Better Audio and Video Experiences with Daily React

Rate this content
Bookmark
Slides

What is WebRTC (Web Real-Time Communication), its main challenges, and how does the combination of React + Daily solve for them? We'll take a deep dive into specifics like React audio and video media elements and Daily React components and hooks. You'll even walk (or code?) out knowing how to build a video grid in just two minutes. 

This talk has been presented at React Day Berlin 2023, check out the latest edition of this React Conference.

Watch video on a separate page

FAQ

Daily React is a React library built on top of Daily's client SDK, designed to simplify the process of building video conferencing apps with WebRTC capabilities.

The main components are App, Call, Prejoin, CallControls, and Tile. Each component has around 30 lines of code, including imports.

You initialize Daily React by wrapping your application with DailyProvider, which accepts configuration options such as URL and Username.

The DailyAudio component sets up audio elements and automatically attaches the appropriate audio tracks at the right time, enabling users to hear each other.

You join a call by calling the daily.join method. This triggers a 'joining meeting' event, indicating that the call setup and connections are being initiated.

Participants are rendered by using the UseParticipantIds hook, which returns an array of unique participant IDs. Mapping through this array allows you to render the actual tiles for each participant.

The useVideoTrack hook allows you to read the local camera state, including whether the camera is on or off. This is useful for toggling the camera on or off.

You leave a call by calling the dailyLeave method, which disconnects the user from the call.

The autoMirror prop mirrors the video for a local user-facing video track, making the displayed video act like a mirror.

You can find the code on Daily's GitHub and all documentation at docs.daily.co. For help or advice, you can reach out to community.daily.co.

Christian Stuff
Christian Stuff
10 min
12 Dec, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Imagine building your own video conferencing app in React using Daily React, a React library built on Daily's client SDK. Rendering components and user controls in the app is crucial, including joining the call, displaying participants, toggling camera and microphone, and leaving the call. Optimize hooks and add features like device pickers, screen sharing, and text chat. Find the code on daily's GitHub and documentation at docs.daily.co.

1. Building a Video Conferencing App in React

Short description:

Imagine building your own video conferencing app in React. WebRTC, the foundation for real-time communication, can be challenging due to browser-specific implementations and differences in behavior. However, with Daily React, a React library built on Daily's client SDK, you can simplify the process. The library provides components like App, Call, Prejoin, CallControls, and Tile, with only around 30 lines of code each. Just wrap your code with DailyProvider and specify the URL and Username, and you're good to go.

Hey there! Imagine you're having a video chat with a good friend of yours 15 minutes from now, but you forgot to send a meeting link along with the calendar invite. I know, I know. There are solutions to this, but what if you could build your very own video conferencing app in React? My name is Christian, I'm a product engineer at Daily, the WebRTC company for developers. And one of my main focuses in working at Daily is to make our APIs as easy as possible to use for React developers.

Coming back to the build-your-own-video-call-app-in-React problem. The web has how-tos, tutorials, and code-alongs in several formats but pretty much all that I've looked at suffer from the same problems. They require a solid amount of code, both on the client and server side, they usually don't handle more than two or three people on a call, and they require to set up infrastructure far beyond a CDN hosting your front-end bundle. So why is WebRTC, the foundation for all of these, so difficult? WebRTC in itself is an open-source project and provides standard APIs for sending audio, video, and data across the web in real-time. In theory, this means that you can use WebRTC APIs on different browsers and operating systems and you should be good. The issue is that each browser vendor maintains their very own implementation of WebRTC and you know what that means? There we dragon. Usually, a WebRTC app will ask for camera and microphone permission in order to further process the media tracks coming from your audio and video devices. GetUserMedia is probably the first API you see mentioned in articles and videos about WebRTC. This API in itself has already interesting differences in its behavior across browsers. As an example, Chrome allows you to re-prompt for audio and video as many times as you like even when access has been blocked. Safari, however, doesn't allow re-prompting so when a user blocks access your best bet is to have them refresh the page.

Now once you've managed to get a media stream from the users' devices, in order to make your app a video conferencing app you'll need to send the stream across the wires otherwise it would just be a mirror app. The RTC Peer Connection API is the tool of choice here but actually getting that connection set up requires a bunch of information. Who are you connecting to? Which codecs are being used for audio and video? What is the available bandwidth? Setting this connection up also includes a negotiation phase where one client, the first one to connect, sends an offer and the remote client answers with an answer. Furthermore, if you don't have a signaling server setup to identify the clients yourself you likely have to reach out to a stun or tone server on the internet to do the client identification process for you. Eventually, when the connection is negotiated and set up you can add the user's audio and video tracks to the connection and if all went well the media bytes should be flowing from one client to the other. Then finally you'll have to access the remote media tracks from the RTC Pure Connection object, attach the tracks to the media elements in the browser and then the users of your app should see each other on screen.

Up until now we haven't thought about any React code. But we are React developers, right? So while all of the things I just mentioned seem complex and overwhelming, and yes, they are, you don't really have to worry about them. This is where I want to introduce Daily React to you. It's a React library built on top of Daily's client SDK and it provides all the things you need to build a React app with WebRTC capabilities. Coming back to the original problem, how can you build a video conferencing app in React? Here's how. The app only has a few components, App, Call, Prejoin, CallControls, and Tile. They all have around 30 lines of code, including imports. Before you are able to utilize any of Daily React's hooks and components, you'll wrap with DailyProvider. It accepts a bunch of configuration options for Call, but for this case, URL and Username are sufficient.

2. Rendering Components and User Controls

Short description:

Users will want to see and hear each other, so rendering the DailyAudio component is crucial. Joining the call is done through daily.join. After calling daily.join, there should be a joining meeting event coming through to indicate that the call machinery is being set up and connections are initiated. UseParticipantIds returns all participants in the call as an array of string IDs. You'll want to know when the meeting ends to switch back to the pre-join screen. Users should be able to toggle their cam and mic. UseVideoTrack allows to read the local cam state. And when clicking the button you'll switch the cam on or off by calling setLocalVideo. When leaving the call all you have to do is call dailyLeave which will disconnect the user from the call. Finally, make sure that your users can see and distinguish each other in the video grid. To render a user's name on their tile, you can use useParticipant property.

Users will want to see and hear each other, so rendering the DailyAudio component is crucial. It sets up audio elements and automatically attaches the right audio tracks at the right time. In adding a little Prejoin screen, you'll allow your users to set a custom username and join the call when ready.

Joining the call is done through daily.join. Daily's Client SDK is an event-driven API framework, so anytime something happens in a call, there's an event emitted with detailed information. After calling daily.join, there should be a joining meeting event coming through to indicate that the call machinery is being set up and connections are initiated. That allows you to switch the view to the call component. This one renders all participants in the call in a little grid and the call controls. UseParticipantIds returns all participants in the call as an array of string IDs. These IDs are unique for each participant and a given meeting session and automatically assigned by daily. And mapping through this array allows you to render the actual tiles.

You'll want to know when the meeting ends to switch back to the pre-join screen. Listening for the left meeting event is the way to go here. Users should be able to toggle their cam and mic. That's like the bare minimum of controls you want to have in a video call. Also a leave button is table stakes for an app like this. For toggling the camera you'll need to know about the camera's track state. Like you can only turn something off when you know it's on, right? UseVideoTrack allows to read the local cam state. The cam state has a handy isOff property that allows to render meaningful text in the toggle cam button. And when clicking the button you'll switch the cam on or off by calling setLocalVideo.

So now that the cam button is functional you'll do the same thing for the mic button. But this time with the help of useAudioTrack and setLocalAudio. When leaving the call all you have to do is call dailyLeave which will disconnect the user from the call. Finally, make sure that your users can see and distinguish each other in the video grid. So here's the tile component. Most importantly you'll want to render the dailyVideo component which will setup the video element with the appropriate video track. The autoMirror prop is pretty cool. It mirrors the video for a local user facing video track. So when you look at your own video it will act like a mirror. To render a user's name on their tile, you can use useParticipant property.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

A Framework for Managing Technical Debt
TechLead Conference 2023TechLead Conference 2023
35 min
A Framework for Managing Technical Debt
Top Content
Today's Talk discusses the importance of managing technical debt through refactoring practices, prioritization, and planning. Successful refactoring requires establishing guidelines, maintaining an inventory, and implementing a process. Celebrating success and ensuring resilience are key to building a strong refactoring culture. Visibility, support, and transparent communication are crucial for addressing technical debt effectively. The team's responsibilities, operating style, and availability should be transparent to product managers.
Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
Watch video: Debugging JS
Debugging JavaScript is a crucial skill that is often overlooked in the industry. It is important to understand the problem, reproduce the issue, and identify the root cause. Having a variety of debugging tools and techniques, such as console methods and graphical debuggers, is beneficial. Replay is a time-traveling debugger for JavaScript that allows users to record and inspect bugs. It works with Redux, plain React, and even minified code with the help of source maps.
Building a Voice-Enabled AI Assistant With Javascript
JSNation 2023JSNation 2023
21 min
Building a Voice-Enabled AI Assistant With Javascript
Top Content
This Talk discusses building a voice-activated AI assistant using web APIs and JavaScript. It covers using the Web Speech API for speech recognition and the speech synthesis API for text to speech. The speaker demonstrates how to communicate with the Open AI API and handle the response. The Talk also explores enabling speech recognition and addressing the user. The speaker concludes by mentioning the possibility of creating a product out of the project and using Tauri for native desktop-like experiences.
Power Fixing React Performance Woes
React Advanced Conference 2023React Advanced Conference 2023
22 min
Power Fixing React Performance Woes
Top Content
Watch video: Power Fixing React Performance Woes
This Talk discusses various strategies to improve React performance, including lazy loading iframes, analyzing and optimizing bundles, fixing barrel exports and tree shaking, removing dead code, and caching expensive computations. The speaker shares their experience in identifying and addressing performance issues in a real-world application. They also highlight the importance of regularly auditing webpack and bundle analyzers, using tools like Knip to find unused code, and contributing improvements to open source libraries.
A Practical Guide for Migrating to Server Components
React Advanced Conference 2023React Advanced Conference 2023
28 min
A Practical Guide for Migrating to Server Components
Top Content
Watch video: A Practical Guide for Migrating to Server Components
React query version five is live and we'll be discussing the migration process to server components using Next.js and React Query. The process involves planning, preparing, and setting up server components, migrating pages, adding layouts, and moving components to the server. We'll also explore the benefits of server components such as reducing JavaScript shipping, enabling powerful caching, and leveraging the features of the app router. Additionally, we'll cover topics like handling authentication, rendering in server components, and the impact on server load and costs.
Monolith to Micro-Frontends
React Advanced Conference 2022React Advanced Conference 2022
22 min
Monolith to Micro-Frontends
Top Content
Microfrontends are considered as a solution to the problems of exponential growth, code duplication, and unclear ownership in older applications. Transitioning from a monolith to microfrontends involves decoupling the system and exploring options like a modular monolith. Microfrontends enable independent deployments and runtime composition, but there is a discussion about the alternative of keeping an integrated application composed at runtime. Choosing a composition model and a router are crucial decisions in the technical plan. The Strangler pattern and the reverse Strangler pattern are used to gradually replace parts of the monolith with the new application.

Workshops on related topic

Build Modern Applications Using GraphQL and Javascript
Node Congress 2024Node Congress 2024
152 min
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
Emanuel Scirlet
Miguel Henriques
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
Building a Shopify App with React & Node
React Summit Remote Edition 2021React Summit Remote Edition 2021
87 min
Building a Shopify App with React & Node
Top Content
WorkshopFree
Jennifer Gray
Hanna Chen
2 authors
Shopify merchants have a diverse set of needs, and developers have a unique opportunity to meet those needs building apps. Building an app can be tough work but Shopify has created a set of tools and resources to help you build out a seamless app experience as quickly as possible. Get hands on experience building an embedded Shopify app using the Shopify App CLI, Polaris and Shopify App Bridge.We’ll show you how to create an app that accesses information from a development store and can run in your local environment.
Build a chat room with Appwrite and React
JSNation 2022JSNation 2022
41 min
Build a chat room with Appwrite and React
WorkshopFree
Wess Cope
Wess Cope
API's/Backends are difficult and we need websockets. You will be using VS Code as your editor, Parcel.js, Chakra-ui, React, React Icons, and Appwrite. By the end of this workshop, you will have the knowledge to build a real-time app using Appwrite and zero API development. Follow along and you'll have an awesome chat app to show off!
Hard GraphQL Problems at Shopify
GraphQL Galaxy 2021GraphQL Galaxy 2021
164 min
Hard GraphQL Problems at Shopify
WorkshopFree
Rebecca Friedman
Jonathan Baker
Alex Ackerman
Théo Ben Hassen
 Greg MacWilliam
5 authors
At Shopify scale, we solve some pretty hard problems. In this workshop, five different speakers will outline some of the challenges we’ve faced, and how we’ve overcome them.

Table of contents:
1 - The infamous "N+1" problem: Jonathan Baker - Let's talk about what it is, why it is a problem, and how Shopify handles it at scale across several GraphQL APIs.
2 - Contextualizing GraphQL APIs: Alex Ackerman - How and why we decided to use directives. I’ll share what directives are, which directives are available out of the box, and how to create custom directives.
3 - Faster GraphQL queries for mobile clients: Theo Ben Hassen - As your mobile app grows, so will your GraphQL queries. In this talk, I will go over diverse strategies to make your queries faster and more effective.
4 - Building tomorrow’s product today: Greg MacWilliam - How Shopify adopts future features in today’s code.
5 - Managing large APIs effectively: Rebecca Friedman - We have thousands of developers at Shopify. Let’s take a look at how we’re ensuring the quality and consistency of our GraphQL APIs with so many contributors.
0 To Auth In An Hour For Your JavaScript App
JSNation 2023JSNation 2023
57 min
0 To Auth In An Hour For Your JavaScript App
WorkshopFree
Asaf Shen
Asaf Shen
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.js backend + Vanilla JS frontend) to authenticate users with One Time Passwords (email) and OAuth, including:
- User authentication – Managing user interactions, returning session / refresh JWTs- Session management and validation – Storing the session securely for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.