Video SDK React SDK Example

repository·main·Indexed 41 days ago

https://github.com/videosdk-live/videosdk-rtc-react-sdk-example

A production-ready React implementation of the Video SDK (version 0.1.0) demonstrating a full-featured video conferencing application. Includes implementations for screen sharing, chat, recording, live streaming, and pre-call device management. Features a comprehensive reference for meeting lifecycles, participant grid layouts, and authentication using temporary or server-side tokens.

Tokens
4K
Snippets
9
Records
18
Agent score
50%

What's inside Video SDK

  1. Meeting controls and UI components

    main

    The meeting interface includes several functional UI areas:

    • Bottom Bar: meeting/components/BottomBar.js provides controls including meeting ID copying, recording indicators, hand raising, microphone/webcam toggles (with device lists), screen sharing, and leaving the meeting. It also includes chat and participant count buttons. On mobile/small screens, it uses a more actions drawer.
    • Presenter View: components/PresenterView.js is used when a participant is sharing their screen.
    • Sidebars:
      • sidebar/ChatPanel.js handles the chat interface (input and message list).
      • sidebar/ParticipantPanel.js displays the list of active participants.
  2. Participant views and layouts

    main

    To display participants in a meeting, the project uses:

    • Individual View: components/ParticipantView.js displays a single participant's video with their name in the corner.
    • Grid Layout: components/ParticipantGrid.js manages a grid of participants. The number of participants displayed in the grid can be managed via meeting/components/ParticipantView.js.
  3. Meeting lifecycle and screen management

    main

    The application manages the meeting lifecycle through several specialized screens:

    • Joining/Creation: components/screens/JoiningScreen.js and components/MeetingDetailsScreen.js allow users to create or join meetings, manage webcam/mic status, and preview video. API calls for meeting validation are located in api.js.
    • Waiting State: components/screens/WaitingToJoinScreen.js displays a Lottie animation while waiting for the isMeetingJoined flag to become true. This flag is provided by the meeting object initialized via the useMeeting() hook from @videosdk.live/react-sdk.
    • Exiting: components/screens/LeaveScreen.js is shown when a participant exits the meeting.
  4. Core Concepts of Video SDK

    main

    To use the SDK effectively, understand these fundamental abstractions:

    • Meeting: Represents the real-time audio and video communication instance. (Note: The terms Room and Meeting are used interchangeably).
    • Sessions: A specific duration spent within a meeting. A single meetingId can have multiple sessions.
    • Participant: Anyone attending a meeting session.
      • local participant: The user currently interacting with the SDK (yourself).
      • remote participants: All other attendees in the session.
    • Stream: The actual video or audio media content published by either the local participant or remote participants.
  5. Configure Authentication with Tokens

    main

    Tokens are required to create, validate, and initialize meetings.

    Development

    For local development, you can generate a temporary token directly from the Video SDK Dashboard. Use this token in your .env file under the key REACT_APP_VIDEOSDK_TOKEN.

    Production

    In production, you must implement an authentication server to authorize users. Do not use temporary tokens in production. For implementation patterns, refer to the videosdk-rtc-api-server-examples repository.

  6. Run the Video SDK React Sample App

    main

    Follow these steps to clone the repository, configure your environment, and launch the production-ready video conferencing example locally.

    Prerequisites

    Setup Steps

    1. Clone the repository
    2. Initialize environment variables: Copy .env.example to .env.
    3. Configure .env: Generate a temporary token from your Video SDK Dashboard and assign it to REACT_APP_VIDEOSDK_TOKEN.
    4. Install dependencies: Run npm install.
    5. Start the app: Run npm run start.
    # 1. Clone
    git clone https://github.com/videosdk-live/videosdk-rtc-react-sdk-example.git
    
    # 2. Set up env
    cp .env.example .env
    
    # 3. Configure .env with REACT_APP_VIDEOSDK_TOKEN="YOUR_TEMPORARY_TOKEN"
    
    # 4. Install
    npm install
    
    # 5. Launch
    npm run start
  7. Pre-call setup and device management

    main

    The example project includes several components to handle device selection and network testing before a user joins a meeting:

    • Audio Input: Use components/DropDown.js to select microphones and monitor audio via the Web Audio API.
    • Camera: Use components/DropDownCam.js to select camera devices and manage permissions.
    • Audio Output: Use components/DropDownSpeaker.js to select speakers and test them with sample sounds.
    • Network Quality: Use components/RunPrecallTest.js to measure uplink/downlink latency, jitter, packet loss, bitrate, frame rate, and resolution.
  8. Use MeetingAppContext to manage meeting state

    main

    The MeetingAppProvider component provides a centralized React Context for managing the state of a video meeting application. By wrapping your application in this provider, you can access and update shared states such as selected media devices, permission statuses, and UI modes using the useMeetingAppContext hook.

    Available States:

    • selectedMic: The currently selected microphone { id, label }.
    • selectedWebcam: The currently selected webcam { id, label }.
    • selectedSpeaker: The currently selected speaker { id, label }.
    • isCameraPermissionAllowed: Boolean or null indicating camera permission status.
    • isMicrophonePermissionAllowed: Boolean or null indicating microphone permission status.
    • raisedHandsParticipants: An array of participants who have raised their hands.
    • sideBarMode: The current mode for the sidebar.
    • pipMode: Boolean indicating if Picture-in-Picture mode is active.

    Available Setters:

    • setSelectedMic, setSelectedWebcam, setSelectedSpeaker
    • setIsCameraPermissionAllowed, setIsMicrophonePermissionAllowed
    • setSideBarMode, setPipMode
    • setRaisedHandsParticipants
    import { MeetingAppProvider, useMeetingAppContext } from './MeetingAppContextDef';
    
    const MyComponent = () => {
      const {
        selectedMic,
        setSelectedMic,
        pipMode,
        setPipMode
      } = useMeetingAppContext();
    
      return (
        <div>
          <p>Current Mic: {selectedMic.label}</p>
          <button onClick={() => setPipMode(!pipMode)}>
            Toggle PiP: {pipMode ? 'ON' : 'OFF'}
          </button>
        </div>
      );
    };
    
    // Wrap your app
    const App = () => (
      <MeetingAppProvider>
        <MyComponent />
      </MeetingAppProvider>
    );
  9. How audio relay works on iOS/iPadOS

    main

    On iOS/iPadOS, the standard HTMLMediaElement.setSinkId() method is silently ignored when playing remote WebRTC tracks because WebKit renders that audio through the WebRTC engine's pipeline, which setSinkId cannot control.

    To enable device output selection (changing speakers) on these devices, the SDK uses an audio relay pattern:

    1. All remote tracks are mixed into a single MediaStreamAudioDestinationNode via an AudioContext.
    2. This mixed stream is played through a single hidden <audio> element (the relay element).
    3. Because the audio is now part of the media-element pipeline rather than the WebRTC engine pipeline, setSinkId() becomes functional, allowing the user to switch audio output devices.
  10. Available Meeting Features Reference

    main

    The sample app demonstrates the following capabilities provided by the Video SDK:

    FeatureDescription
    Precall SetupConfigure audio, video devices, and settings before joining.
    Waiting LobbyA virtual space for participants to wait before joining.
    Join/Leave MeetingStandard meeting entry and exit flows.
    Media ControlsToggle Mic, Toggle Camera, and Switch Input/Output devices.
    Screen ShareShare your screen with other participants.
    Image CaptureCapture images from participant video streams (useful for KYC).
    Track OptimizationEnhance media track quality and performance.
    CollaborationIn-meeting Chat (PubSub), Whiteboard, and File Sharing.
    RecordingRecord meetings for future reference.
    Live StreamingRTMP Livestream to platforms like YouTube or Facebook.
    TranscriptionReal-time transcription of meeting audio.
    Participant ManagementRemote Media Control, Mute All, and Remove Participant.
  11. Set the audio output device via setRelaySinkId

    main

    Use setRelaySinkId(deviceId) to change the audio output device (e.g., switching from built-in speakers to a Bluetooth headset).

    Note: This only functions if the audio relay is active (which requires an iOS/iPadOS device where setSinkId is supported). If called on unsupported devices, the function returns without action.

    import { setRelaySinkId } from './utils/audioOutputRelay';
    
    // deviceId is the unique identifier for the audio output device
    setRelaySinkId('some-device-id');