In this tutorial, we will guide you through integrating a video huddle feature into your react application using SuperViz. This tutorial will use the dedicated React SDK from SuperViz to build the application.
Video huddles are essential for real-time communication and collaboration, enabling users to interact via video conferencing directly within your application. This feature is perfect for remote teams, educational platforms, and any scenario where visual communication enhances collaboration. Let's get started!
Prerequisite
To follow this tutorial, you will need a SuperViz account and a developer token. If you already have an account and a developer token, you can move on to the next step.
Create an account
To create an account, go to https://dashboard.superviz.com/register and create an account using either Google or an email/password. It's important to note that when using an email/password, you will receive a confirmation link that you'll need to click to verify your account.
Retrieving a Developer Token
To use the SDK, you’ll need to provide a developer token, as this token is essential for associating SDK requests with your account. You can retrieve both development and production SuperViz tokens from the dashboard..
Copy and save the developer token, as you will need it in the next steps of this tutorial.
Step 1: Set Up Your React Application
To begin, you'll need to set up a new React project where we will integrate the SuperViz SDK for video huddles.
1. Create a New React Project
First, create a new React application using Create React App with TypeScript.
1npm create vite@latest video-huddle-app -- --template react-ts2cd video-huddle-app
2. Install SuperViz SDK
Next, install SuperViz, which will enable us to add video conferencing features to our application.
1npm install @superviz/react @superviz/video react-icons uuid
- @superviz/react: The SuperViz core react package that contains the RoomProvider hooks.
- @superviz/video: package that contains the video huddle component.
- react-icons: A library for including icons in React applications, used here for a call button icon.
- uuid: A library for generating unique identifiers, used for the participant ID.
3. Configure tailwind
In this tutorial, we'll use the Tailwind css framework. First, install the tailwind package.
1npm install -D tailwindcss postcss autoprefixer2npx tailwindcss init -p
We then need to configure the template path. Open tailwind.config.js
in the root of the project and insert the following code.
1/** @type {import('tailwindcss').Config} */2export default {3content: [4"./index.html",5"./src/**/*.{js,ts,jsx,tsx}",6],7theme: {8extend: {},9},10plugins: [],11}
Then we need to add the tailwind directives to the global CSS file. (src/index.css)
1@tailwind base;2@tailwind components;3@tailwind utilities;
4. Set Up Environment Variables
Create a .env
file in your project root and add your SuperViz developer key. This key will be used to authenticate your application with SuperViz services.
1VITE_SUPERVIZ_API_KEY=YOUR_SUPERVIZ_DEVELOPER_KEY
Step 2: Implement the Main Application
In this step, we'll implement the main application logic to initialize the room and video huddle.
1. Implement the App Component
Open src/App.tsx
and set up the main application component using SuperViz to manage the collaborative environment.
1import { RoomProvider, useRoom, useVideo } from '@superviz/react'2import { VideoHuddle, MeetingState } from "@superviz/video";34import { useState } from "react";5import { ImSpinner2 } from "react-icons/im";6import { v4 as generateId } from 'uuid'
Explanation:
- Imports: Import necessary components and hooks from SuperViz, React, uuid, and React Icons.
2. Define Constants
Define constant for the DEVELOPER_TOKEN.
1const DEVELOPER_TOKEN = import.meta.env.VITE_SUPERVIZ_API_KEY;
Explanation:
- apiKey: Retrieves the SuperViz API key from environment variables.
3. Add the Room Provider
Add the Room Provider to the main App
component.
1export function App() {2return (3<RoomProvider developerToken={DEVELOPER_TOKEN}>4<Children />5</RoomProvider>6)7}
Explanation:
- RoomProvider: A component that provides the SuperViz Room context to the application.
- Children: A component representing the main content of the application that will be rendered inside the RoomProvider.
4. Create the Children Component
Set up the main App
component and initialize state variables.
1export const Children = () => {2const [isLoading, setIsLoading] = useState(false);3const [meetingEnded, setMeetingEnded] = useState(false);4const [meetingStarted, setMeetingStarted] = useState(false);
Explanation:
- isLoading: A state variable to track whether the the huddle is loading.
- meetingEnded: A state variable to track whether the meeting has ended.
- meetingStarted: A state variable to track whether the meeting has started.
5.Add hooks
Add the SuperViz userRoom hook and the Video useVideo Hook.
1const { joinRoom, addComponent } = useRoom();23useVideo({4onMeetingStateUpdate: (state: MeetingState) => {5if (state === MeetingState.MEETING_READY_TO_JOIN) setIsLoading(false);6},7onParticipantLeft: () => setMeetingEnded(true),8onParticipantJoined: () => setMeetingStarted(true),9});
Explanation:
- useRoom: A hook to join the room and add components to the room.
- useVideo: A hook to manage the video conference component.
6. Initialize video huddle
Create an initialize
function to set up the SuperViz environment.
1const initialize = async () => {2setIsLoading(true);34try {5await joinRoom({6participant: {7id: generateId(),8name: " ",9},10group: {11name: "GROUP_NAME",12id: "GROUP_ID",13},14roomId: "ROOM_ID",15});1617const video = new VideoHuddle({18participantType: 'host',19brand: {20logoUrl: "https://docs.superviz.com/logo-white.svg",21},22});2324addComponent(video);2526} catch (error) {27console.error("Error initializing SuperViz Room:", error);28}29};
Explanation:
- initialize: An asynchronous function that initializes the SuperViz room
- joinRoom: Joins the SuperViz room with the specified participant, group, and room details.
- VideoHuddle: Creates a video conference component, setting the participant type to 'host'.
- addComponent: Adds the video conference component to the SuperViz room.
7. Render the application
Return the JSX structure for rendering the application.
1return (2<div className="h-screen flex items-center justify-center bg-[#121212]">3{isLoading ? (4<ImSpinner2 className="text-4xl text-white animate-spin" />5) : meetingEnded ? (6<div className="text-lg font-semibold text-white">Thank you for joining the video huddle</div>7) : meetingStarted ? (8<div className="text-lg font-semibold text-white">Content going here</div>9): (10<button11className="bg-[#6210cc] text-white px-5 py-3 text-xs rounded-lg"12onClick={initialize}13>14START VIDEO HUDDLE15</button>16)}17</div>18);
Explanation:
- isLoading state: Shows a loading spinner when the huddle is loading.
- meetingEnded state: Shows a message when the meeting has ended.
- meetingStarted state: Shows the content of the application when the meeting has started.
- button: Renders a button that calls the
initialize
function.
Step 4: Running the Application
1. Start the React Application
To run your application, use the following command in your project directory:
1npm run dev
This command will start the development server and open your application in the default web browser. You can initiate a video huddle and invite other participants to join.
2. Test the Application
- Video Huddle: Click the video call button to start the video huddle. Open the application in multiple browser windows or tabs to simulate multiple participants joining the huddle.
Summary
In this tutorial, we integrated a video huddle feature into a web application using SuperViz. We configured a React application to handle video huddle, enabling multiple users to join and participate in a video call seamlessly. This setup can be extended and customized to fit various scenarios where real-time communication and collaboration are required.
Feel free to explore the full code and further examples in the GitHub repository for more details.