mediasfu-reactjs
Version:
MediaSFU Prebuilt ReactJS SDK - Compatible with React 18 & 19, TypeScript & JavaScript
1,447 lines (1,163 loc) β’ 200 kB
Markdown
<p align="center">
<img src="https://www.mediasfu.com/logo192.png" width="100" alt="MediaSFU Logo">
</p>
<p align="center">
<a href="https://twitter.com/media_sfu">
<img src="https://img.shields.io/badge/Twitter-1DA1F2?style=for-the-badge&logo=twitter&logoColor=white" alt="Twitter" />
</a>
<a href="https://www.mediasfu.com/forums">
<img src="https://img.shields.io/badge/Community-Forum-blue?style=for-the-badge&logo=discourse&logoColor=white" alt="Community Forum" />
</a>
<a href="https://github.com/MediaSFU">
<img src="https://img.shields.io/badge/GitHub-181717?style=for-the-badge&logo=github&logoColor=white" alt="Github" />
</a>
<a href="https://www.mediasfu.com/">
<img src="https://img.shields.io/badge/Website-4285F4?style=for-the-badge&logo=google-chrome&logoColor=white" alt="Website" />
</a>
<a href="https://www.youtube.com/channel/UCELghZRPKMgjih5qrmXLtqw">
<img src="https://img.shields.io/badge/YouTube-FF0000?style=for-the-badge&logo=youtube&logoColor=white" alt="Youtube" />
</a>
</p>
<p align="center">
<a href="https://opensource.org/licenses/MIT">
<img src="https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square" alt="License: MIT" />
</a>
<a href="https://mediasfu.com">
<img src="https://img.shields.io/badge/Built%20with-MediaSFU-blue?style=flat-square" alt="Built with MediaSFU" />
</a>
<a href="https://reactjs.org">
<img src="https://img.shields.io/badge/React-20232A?style=flat-square&logo=react&logoColor=61DAFB" alt="React" />
</a>
<a href="https://www.typescriptlang.org">
<img src="https://img.shields.io/badge/TypeScript-007ACC?style=flat-square&logo=typescript&logoColor=white" alt="TypeScript" />
</a>
</p>
---
## π¨ **BREAKING: AI Phone Agents at $0.10 per 1,000 minutes**
π **Call our live AI demos right now:**
- πΊπΈ **+1 (785) 369-1724** - Mixed Support Demo
- π¬π§ **+44 7445 146575** - AI Conversation Demo
- π¨π¦ **+1 (587) 407-1990** - Technical Support Demo
- π¨π¦ **+1 (647) 558-6650** - Friendly AI Chat Demo
**Traditional providers charge $0.05 per minute. We charge $0.10 per 1,000 minutes. That's 500x cheaper.**
β
**Deploy AI phone agents in 30 minutes**
β
**Works with ANY SIP provider** (Twilio, Telnyx, Zadarma, etc.)
β
**Seamless AI-to-human handoffs**
β
**Real-time call analytics & transcription**
π **[Complete SIP/PSTN Documentation β](https://mediasfu.com/telephony)**
---
## Quick Reference: Component Props & UI Overrides
> **New:** UI override parity now extends across Webinar and Chat layouts, unifying customization for every MediaSFU interface.
Every primary MediaSFU UI exportβ`MediasfuGeneric`, `MediasfuBroadcast`, `MediasfuConference`, `MediasfuWebinar`, and `MediasfuChat`βnow ships with a consistent prop surface and a powerful `uiOverrides` map, so you can bend the bundled experience to match your product without losing MediaSFUβs hardened real-time logic.
### Shared component props (applies to every MediaSFU UI component)
| Prop | Type | Default | What it does |
| --- | --- | --- | --- |
| `PrejoinPage` | `(options) => React.ReactNode` | `WelcomePage` | Swap in a custom pre-join experience. Receives unified pre-join options so you can add branding, legal copy, or warm-up flows. |
| `localLink` | `string` | `""` | Point the SDK at your self-hosted MediaSFU server. Leave empty when using MediaSFU Cloud. |
| `connectMediaSFU` | `boolean` | `true` | Toggle automatic socket/WebRTC connections. Set to `false` when you only need the UI shell. |
| `credentials` | `{ apiUserName: string; apiKey: string }` | `{ apiUserName: "", apiKey: "" }` | Supply cloud credentials without hard-coding them elsewhere. |
| `useLocalUIMode` | `boolean` | `false` | Run the interface in local/demo mode with no remote signaling. |
| `seedData`, `useSeed` | `SeedData`, `boolean` | `{}`, `false` | Pre-populate the UI for demos, snapshot tests, or onboarding tours. |
| `imgSrc` | `string` | `https://mediasfu.com/images/logo192.png` | Default artwork used across pre-join and modal flows. |
| `sourceParameters` | `Record<string, unknown>` | `undefined` | Shared helper bag (media devices, participant helpers, layout handlers). Pair with `updateSourceParameters` to mirror the SDKβs internal utilities. |
| `updateSourceParameters` | `(helpers) => void` | `undefined` | Receive the latest helper bundle so you can bridge MediaSFU logic into your own components. |
| `returnUI` | `boolean` | `true` | When `false`, mount the logic onlyβa perfect stepping stone to a fully bespoke interface. |
| `noUIPreJoinOptions` | `CreateMediaSFURoomOptions \| JoinMediaSFURoomOptions` | `undefined` | Feed pre-join data when `returnUI` is `false` and you want to bypass the on-screen wizard. |
| `joinMediaSFURoom`, `createMediaSFURoom` | Functions | `undefined` | Inject your own networking layers for joining or creating rooms. |
| `customComponent` | `CustomComponentType` | `undefined` | Replace the entire UI while retaining transports, sockets, and helpers. |
| `customVideoCard`, `customAudioCard`, `customMiniCard` | Factories | `undefined` | Override participant card renders to add metadata, CTAs, or badges. |
| `containerStyle` | `React.CSSProperties` | `undefined` | Apply inline styles to the root wrapper (dashboards, split views, etc.). |
| `uiOverrides` | `MediasfuUICustomOverrides` | `undefined` | Targeted component/function overrides described below. |
> **Power combo:** Set `returnUI={false}` to run MediaSFU logic headless, capture helpers via `updateSourceParameters`, and selectively bring UI pieces back with `uiOverrides`. That gives you progressive migration with minimal code churn.
```ts
import type { MediasfuUICustomOverrides } from "mediasfu-reactjs";
const overrides: MediasfuUICustomOverrides = { /* ... */ };
```
Bring the types into your project to unlock full IntelliSense for every override slot.
### Custom UI Playbook
Use a toggle-driven "playbook" component to experiment with MediaSFUβs customization layers. Flip a couple of booleans and you can watch the SDK jump between prebuilt layouts, headless logic, or a fully bespoke workspace driven by `customComponent`.
#### What the playbook demonstrates
- **Connection presets**: toggle `connectionScenario` between `cloud`, `hybrid`, or `ce` to swap credentials, local links, and connection modes in one place.
- **Experience selector**: the `selectedExperience` switch renders `MediasfuGeneric`, `MediasfuBroadcast`, `MediasfuWebinar`, `MediasfuConference`, or `MediasfuChat` without touching the rest of your stack.
- **UI strategy flags**: booleans like `showPrebuiltUI`, `enableFullCustomUI`, and `enableNoUIPreJoin` demonstrate how to run the MediaSFU logic with or without the bundled UI.
- **Layered overrides**: toggles enable the custom video/audio/mini cards, drop-in `uiOverrides` for layout and modal surfaces, container styling, and backend proxy helpers.
- **Custom workspace demo**: a `customComponent` receives live MediaSFU helpers so you can build dashboards, CRM surfaces, or any bespoke host interface.
- **Debug panel & helpers**: optional JSON panel exposes the `updateSourceParameters` payload so you can see exactly what to wire into your own components.
#### Try it quickly
```tsx
const connectionScenario: "cloud" | "hybrid" | "ce" = "cloud";
const selectedExperience = "generic" as const;
const showPrebuiltUI = true;
const enableFullCustomUI = false;
const connectionPresets = {
cloud: { credentials: { apiUserName: "demo", apiKey: "demo" }, localLink: "", connectMediaSFU: true },
hybrid: { credentials: { apiUserName: "demo", apiKey: "demo" }, localLink: "http://localhost:3000", connectMediaSFU: true },
ce: { credentials: undefined, localLink: "http://localhost:3000", connectMediaSFU: false },
};
const Experience = {
generic: MediasfuGeneric,
broadcast: MediasfuBroadcast,
webinar: MediasfuWebinar,
conference: MediasfuConference,
chat: MediasfuChat,
}[selectedExperience];
export const CustomUIPlaybook = () => {
const overrides = useMemo(() => ({
mainContainer: enableFullCustomUI
? {
render: (props) => (
<div style={{ border: "4px dashed purple", borderRadius: 24, padding: 16 }}>
<MainContainerComponent {...props} />
</div>
),
}
: undefined,
}), [enableFullCustomUI]);
const current = connectionPresets[connectionScenario];
return (
<Experience
{...current}
showPrebuiltUI={showPrebuiltUI}
uiOverrides={overrides}
containerStyle={{ background: "linear-gradient(135deg, #0f172a, #1e3a8a)", minHeight: "100vh" }}
/>
);
};
```
Toggle the configuration values at the top of the playbook and watch the UI reconfigure instantly. Itβs the fastest path to understand MediaSFUβs override surface before you fold the patterns into your production entrypoint.
#### Passing custom props and UI overrides
Use the same playbook to validate bespoke cards, override bundles, and fully custom workspaces before you move them into production code:
```tsx
const videoCard: CustomVideoCardType = (props) => (
<VideoCard
{...props}
customStyle={{
borderRadius: 20,
border: "3px solid #4c1d95",
boxShadow: "0 28px 65px rgba(76,29,149,0.35)",
background: "linear-gradient(140deg, rgba(15,23,42,0.78), rgba(30,64,175,0.45))",
...(props.customStyle ?? {}),
}}
/>
);
const audioCard: CustomAudioCardType = (props) => (
<AudioCard
{...props}
barColor="#22c55e"
customStyle={{ borderRadius: 22, background: "rgba(34,197,94,0.1)" }}
/>
);
const miniCard: CustomMiniCardType = (props) => (
<MiniCard
{...props}
renderContainer={({ defaultContainer }) => (
<div style={{ display: "grid", placeItems: "center", height: "100%" }}>{defaultContainer}</div>
)}
/>
);
const uiOverrides = useMemo<MediasfuUICustomOverrides>(() => ({
mainContainer: {
render: (props) => (
<div style={{ border: "4px dashed rgba(139,92,246,0.8)", borderRadius: 28, padding: 16 }}>
<MainContainerComponent {...props} />
</div>
),
},
menuModal: {
component: (modalProps) => <MenuModal {...modalProps} variant="glass" />,
},
consumerResume: {
wrap: (original) => async (params) => {
const startedAt = performance.now();
const result = await original(params);
analytics.track("consumer_resume", {
durationMs: performance.now() - startedAt,
consumerId: params?.consumer?.id,
});
return result;
},
},
}), []);
return (
<Experience
{...current}
customVideoCard={videoCard}
customAudioCard={audioCard}
customMiniCard={miniCard}
customComponent={enableFullCustomUI ? CustomWorkspace : undefined}
containerStyle={{ background: "#0f172a", borderRadius: 32, overflow: "hidden" }}
uiOverrides={uiOverrides}
/>
);
```
Because the playbook surfaces `updateSourceParameters`, you can also log or snapshot the helper bundle (`getParticipantMedia`, `toggleMenuModal`, `showAlert`, and more) to ensure your custom UI always receives the hooks it expects.
### `uiOverrides` map β override keys at a glance
Each key accepts a `CustomComponentOverride<Props>` object with optional `component` and `render` fields. You can fully replace the default implementation or wrap it while forwarding props.
#### Layout & control surfaces
| Key | Default component | Typical use |
| --- | --- | --- |
| `mainContainer` | `MainContainerComponent` | Inject theming providers or dashboard layouts. |
| `mainAspect` | `MainAspectComponent` | Tune how the main region splits space. |
| `mainScreen` | `MainScreenComponent` | Orchestrate hero video + gallery interplay. |
| `mainGrid` | `MainGridComponent` | Modify layout or layering of primary participants. |
| `subAspect` | `SubAspectComponent` | Restyle fixed control strips in webinar/conference modes. |
| `otherGrid` | `OtherGridComponent` | Change presentation of off-stage attendees. |
| `flexibleGrid`, `flexibleGridAlt` | `FlexibleGrid` | Implement AI-driven or branded array layouts. |
| `flexibleVideo` | `FlexibleVideo` | Add captions, watermarks, or overlays to highlighted speakers. |
| `audioGrid` | `AudioGrid` | Customise audio-only attendee presentation. |
| `pagination` | `Pagination` | Introduce infinite scroll or auto-cycling carousels. |
| `controlButtons` | `ControlButtonsComponent` | Rebrand the primary action bar. |
| `controlButtonsAlt` | `ControlButtonsAltComponent` | Control secondary button clusters. |
| `controlButtonsTouch` | `ControlButtonsComponentTouch` | Deliver mobile-first controls (used heavily by `MediasfuChat`). |
#### Participant cards & widgets
| Key | Default component | Typical use |
| --- | --- | --- |
| `videoCard` | `VideoCard` | Add host badges, reactions, or CRM overlays. |
| `audioCard` | `AudioCard` | Swap avatars or expose spoken-language info. |
| `miniCard` | `MiniCard` | Customize thumbnails in picture-in-picture modes. |
| `miniAudio` | `MiniAudio` | Re-style the audio-only mini indicators. |
| `meetingProgressTimer` | `MeetingProgressTimer` | Replace the elapsed-time widget with countdowns or milestones. |
| `miniAudioPlayer` | `MiniAudioPlayer` | Provide alternative UI for recorded clip playback. |
#### Modals, dialogs, and collaboration surfaces
| Key | Default component | Typical use |
| --- | --- | --- |
| `loadingModal` | `LoadingModal` | Show branded skeletons while connecting. |
| `alert` | `AlertComponent` | Route alerts through your notification system. |
| `menuModal` | `MenuModal` | Redesign quick-action trays. |
| `eventSettingsModal` | `EventSettingsModal` | Extend host tools with your own settings. |
| `requestsModal` | `RequestsModal` | Build moderation queues tailored to your workflows. |
| `waitingRoomModal` | `WaitingRoomModal` | Deliver custom waiting-room experiences. |
| `coHostModal` | `CoHostModal` | Manage co-hosts with bespoke UX. |
| `mediaSettingsModal` | `MediaSettingsModal` | Embed device tests or instructions. |
| `participantsModal` | `ParticipantsModal` | Introduce advanced filters, search, or notes. |
| `messagesModal` | `MessagesModal` | Drop in your full-featured chat module. |
| `displaySettingsModal` | `DisplaySettingsModal` | Let users pick layouts, themes, or captions. |
| `confirmExitModal` | `ConfirmExitModal` | Meet compliance wording requirements. |
| `confirmHereModal` | `ConfirmHereModal` | Customize attendance confirmations for webinars. |
| `shareEventModal` | `ShareEventModal` | Add referral codes or QR sharing. |
| `recordingModal` | `RecordingModal` | Tailor recording confirmation flows. |
| `pollModal` | `PollModal` | Integrate your polling/quiz engine. |
| `backgroundModal` | `BackgroundModal` | Hook AI background replacement or brand presets. |
| `breakoutRoomsModal` | `BreakoutRoomsModal` | Implement drag-and-drop or AI room suggestions. |
| `configureWhiteboardModal` | `ConfigureWhiteboardModal` | Adjust collaboration permissions before launch. |
| `whiteboard` | `Whiteboard` | Replace with your whiteboard provider. |
| `screenboard` | `Screenboard` | Modify shared-screen annotation layers. |
| `screenboardModal` | `ScreenboardModal` | Reimagine how users enable shared annotations. |
#### Entry flows & custom renderers
| Key | Default component | Typical use |
| --- | --- | --- |
| `welcomePage` | `WelcomePage` | Provide a fully branded welcome/marketing splash. |
| `preJoinPage` | `PrejoinPage` | Override the wizard used before joining live sessions. |
| `customMenuButtonsRenderer` | `ControlButtonsAltComponent` | Supply a bespoke renderer for menu button groups without overriding each button. |
#### Function overrides
| Key | Default function | Typical use |
| --- | --- | --- |
| `consumerResume` | `consumerResume` | Wrap errors, capture analytics, or rate-limit consumer resume behavior. |
| `addVideosGrid` | `addVideosGrid` | Replace participant ordering or layout heuristics on the fly. |
> Function overrides support `{ implementation, wrap }`. Provide `implementation` for a full replacement, or `wrap` to intercept the default behavior before/after it runs.
### Example: swap the chat modal and theme the controls
```tsx
import { MediasfuGeneric } from "mediasfu-reactjs";
import { MyChatModal } from "./ui/MyChatModal";
import { MyControls } from "./ui/MyControls";
const uiOverrides = {
messagesModal: {
component: MyChatModal,
},
controlButtons: {
render: (props) => <MyControls {...props} variant="glass" />,
},
};
export const MyMeeting = () => (
<MediasfuGeneric credentials={credentials} uiOverrides={uiOverrides} />
);
```
### Example: wrap a MediaSFU helper instead of replacing it
```tsx
const uiOverrides = {
consumerResume: {
wrap: (original) => async (params) => {
const startedAt = performance.now();
const result = await original(params);
analytics.track("consumer_resume", {
durationMs: performance.now() - startedAt,
consumerId: params?.consumer?.id,
});
return result;
},
},
};
<MediasfuConference uiOverrides={uiOverrides} />;
```
The same override hooks power the newly refreshed `MediasfuWebinar` and `MediasfuChat` layouts, so you can guarantee a unified experience across events, webinars, or chat-first rooms.
---
MediaSFU offers a cutting-edge streaming experience that empowers users to customize their recordings and engage their audience with high-quality streams. Whether you're a content creator, educator, or business professional, MediaSFU provides the tools you need to elevate your streaming game.
<div style="text-align: center;">
<img src="https://mediasfu.com/images/header_1.jpg" alt="Preview Page" title="Preview Page" style="max-height: 600px;">
</div>
---
# MediaSFU ReactJS Module Documentation
## Unlock the Power of MediaSFU Community Edition
**MediaSFU Community Edition is free and open-source**βperfect for developers who want to run their own media server without upfront costs. With robust features and simple setup, you can launch your media solution in minutes. **Ready to scale?** Upgrade seamlessly to **MediaSFU Cloud** for enterprise-grade performance and global scalability.
**[Get started now on GitHub!](https://github.com/MediaSFU/MediaSFUOpen)**
### β
ReactJS SDK Setup Guide
[](https://www.youtube.com/watch?v=VvulSNB_AYg)
π₯ [**Watch the ReactJS SDK Setup Guide**](https://youtu.be/VvulSNB_AYg)
---
## Table of Contents
- [Quick Reference: Component Props & UI Overrides](#quick-reference-component-props--ui-overrides)
- [Custom UI Playbook](#custom-ui-playbook)
- [Features](#features)
- [Getting Started](#getting-started)
- [π ReactJS SDK Guide](#reactjs-sdk-guide)
- [Quick Start](#quick-start-5-minutes)
- [Understanding the Architecture](#understanding-mediasfu-architecture)
- [Core Concepts & Components](#core-concepts--components)
- [Working with Methods](#working-with-methods)
- [Media Streams & Participants](#media-streams--participants)
- [Customization & Styling](#customization--styling)
- [API Reference](#api-reference)
- [Troubleshooting](#troubleshooting)
- [Contributing](#contributing)
# Features <a name="features"></a>
MediaSFU's React SDK comes with a host of powerful features out of the box:
1. **Screen Sharing with Annotation Support**: Share your screen with participants and annotate in real-time for enhanced presentations and collaborations.
2. **Collaborative Whiteboards**: Create and share whiteboards for real-time collaborative drawing and brainstorming sessions.
3. **Breakout Rooms**: Create multiple sub-meetings within a single session to enhance collaboration and focus.
4. **Pagination**: Efficiently handle large participant lists with seamless pagination.
5. **Polls**: Conduct real-time polls to gather instant feedback from participants.
6. **Media Access Requests Management**: Manage media access requests with ease to ensure smooth operations.
7. **Video Effects**: Apply various video effects, including virtual backgrounds, to enhance the visual experience.
8. **Chat (Direct & Group)**: Facilitate communication with direct and group chat options.
9. **Cloud Recording (track-based)**: Customize recordings with track-based options, including watermarks, name tags, background colors, and more.
10. **Managed Events**: Manage events with features to handle abandoned and inactive participants, as well as enforce time and capacity limits.
## π **New Advanced Media Access**
**Interested in getting just the media stream of a specific participant?** You can now easily retrieve individual participant streams using `sourceParameters.getParticipantMedia()` [Learn more β](#media-device-and-stream-utility-methods)
**Need to access available cameras and microphones?** Use `sourceParameters.getMediaDevicesList()` to enumerate all available media devices on the user's system programmatically.
# Getting Started <a name="getting-started"></a>
This section will guide users through the initial setup and installation of the npm module.
### Documentation Reference
For comprehensive documentation on the available methods, components, and functions, please visit [mediasfu.com](https://www.mediasfu.com/reactjs/). This resource provides detailed information for this guide and additional documentation.
## Installation
Instructions on how to install the module using npm.
### 1. Add the Package to Your Project
```bash
npm install mediasfu-reactjs
```
#### **1.1 Important Installation Notes**
#### π« **Avoid Using `--force` or `--legacy-peer-deps`**
Using these flags can override important dependency checks, potentially causing **unstable builds** or **unexpected behavior**.
- **Why Avoid Them?**
They bypass compatibility checks, which can introduce **bugs** or **conflicts** within your project.
---
#### βοΈ **Use Package Overrides (Recommended)**
If you encounter **peer dependency conflicts**, use the `overrides` field in your `package.json` instead of forcing installations.
##### β
**Example of Safe Overrides:**
```json
{
"overrides": {
"some-package": {
"dependency-name": "^1.2.3"
}
}
}
```
- **Why This Works:**
Overrides let you resolve conflicts **safely** without compromising the integrity of your project.
---
#### π© **If You Absolutely Need to Use `--force` or `--legacy-peer-deps`**
- Some peer dependencies **might be skipped**.
- Youβll need to **manually install** them to avoid runtime errors.
##### π **Install the Required Peer Dependencies:**
```bash
npm install \
"@fortawesome/fontawesome-svg-core@^6.6.0" \
"@fortawesome/free-brands-svg-icons@^6.6.0" \
"@fortawesome/free-solid-svg-icons@^6.6.0" \
"@fortawesome/react-fontawesome@^0.2.2" \
"@mediapipe/selfie_segmentation@0.1.1675465747" \
"bootstrap@^5.3.3" \
"mediasoup-client@^3.16.0" \
"react@^19.0.0" \
"react-dom@^19.0.0" \
"socket.io-client@4.8.0" \
"universal-cookie@^7.2.0"
```
- **Why This Is Important:**
These peer dependencies are critical for `mediasfu-reactjs` to function correctly.
---
#### π **How to Check for Peer Dependencies**
1. **Open your `package.json`.**
2. Look for the `peerDependencies` section:
```json
"peerDependencies": {
"@fortawesome/fontawesome-svg-core": "^6.6.0",
"@fortawesome/free-brands-svg-icons": "^6.6.0",
"@fortawesome/free-solid-svg-icons": "^6.6.0",
"@fortawesome/react-fontawesome": "^0.2.2",
"@mediapipe/selfie_segmentation": "0.1.1675465747",
"bootstrap": "^5.3.3",
"mediasoup-client": "^3.16.0",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"socket.io-client": "4.8.0",
"universal-cookie": "^7.2.0"
}
```
3. **Ensure all are installed.** If not, run the install command above.
---
#### β
**Final Recommendations:**
- Always try to resolve conflicts using **overrides** first.
- Only use `--force` or `--legacy-peer-deps` as a **last resort**.
---
### 2. Obtain an API Key (If Required)
You can get your API key by signing up or logging into your account at [mediasfu.com](https://www.mediasfu.com/).
<div style="background-color:#f0f0f0; padding: 10px; border-radius: 5px;">
<h4 style="color:#d9534f;">Important:</h4>
<p style="font-size: 1.2em; color: black;">You must obtain an API key from <a href="https://www.mediasfu.com/">mediasfu.com</a> to use this package with MediaSFU Cloud. You do not need the API Key if self-hosting.</p>
</div>
## **Self-Hosting MediaSFU**
If you plan to self-host MediaSFU or use it without MediaSFU Cloud services, you don't need an API key. You can access the open-source version of MediaSFU from the [MediaSFU Open Repository](https://github.com/MediaSFU/MediaSFUOpen).
This setup allows full flexibility and customization while bypassing the need for cloud-dependent credentials.
---
# π ReactJS SDK Guide <a name="reactjs-sdk-guide"></a>
This comprehensive guide will walk you through everything you need to know about building real-time communication apps with MediaSFU's ReactJS SDK. Whether you're a beginner or an experienced developer, you'll find clear explanations, practical examples, and best practices.
---
## Quick Start (5 Minutes) <a name="quick-start-5-minutes"></a>
Get your first MediaSFU app running in just a few minutes.
### Step 1: Install the Package
```bash
npm install mediasfu-reactjs
```
### Step 2: Import and Use
```javascript
import React from 'react';
import { MediasfuGeneric } from 'mediasfu-reactjs';
function App() {
// Option 1: Use without credentials (for testing)
return <MediasfuGeneric />;
// Option 2: Use with MediaSFU Cloud credentials
// const credentials = { apiUserName: 'your_username', apiKey: 'your_api_key' };
// return <MediasfuGeneric credentials={credentials} />;
}
export default App;
```
### Step 3: Run Your App
```bash
npm start
```
**That's it!** You now have a fully functional video conferencing app with:
- β
Video and audio streaming
- β
Screen sharing
- β
Chat messaging
- β
Participant management
- β
Recording capabilities
- β
Breakout rooms
- β
Polls and whiteboards
---
## Understanding MediaSFU Architecture <a name="understanding-mediasfu-architecture"></a>
Before diving deeper, let's understand how MediaSFU is structured.
### The Three-Layer Architecture
```
βββββββββββββββββββββββββββββββββββββββββββββββ
β Your React Application β
β (App.js, components, business logic) β
βββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββ
β MediaSFU Components Layer β
β (MediasfuGeneric, MediasfuBroadcast, etc.) β
β - Pre-built UI components β
β - Event handling β
β - State management β
βββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββ
β MediaSFU Core Methods Layer β
β (Stream control, room management, β
β WebRTC handling, socket communication) β
βββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββ
β MediaSFU Backend Services β
β (MediaSFU Cloud or Community Edition) β
βββββββββββββββββββββββββββββββββββββββββββββββ
```
### Key Concepts
#### 1. **Event Room Types**
MediaSFU provides 5 specialized room types, each optimized for specific use cases:
| Room Type | Best For | Key Features |
|-----------|----------|--------------|
| **MediasfuGeneric** | General purpose meetings | Flexible layout, all features enabled |
| **MediasfuBroadcast** | Live streaming events | Optimized for one-to-many communication |
| **MediasfuWebinar** | Educational sessions | Presenter focus, Q&A features |
| **MediasfuConference** | Business meetings | Equal participant layout, collaboration tools |
| **MediasfuChat** | Interactive discussions | Chat-first interface, quick connections |
```javascript
// Choose the right room type for your use case
import { MediasfuWebinar, MediasfuBroadcast, MediasfuConference } from 'mediasfu-reactjs';
// For a webinar
<MediasfuWebinar credentials={credentials} />
// For a broadcast
<MediasfuBroadcast credentials={credentials} />
// For a conference
<MediasfuConference credentials={credentials} />
```
#### 2. **The Three Usage Modes**
MediaSFU offers three progressive levels of customization:
##### Mode 1: Default UI (Simplest)
Use MediaSFU's complete pre-built interface - perfect for rapid development.
```javascript
import { MediasfuGeneric } from 'mediasfu-reactjs';
function App() {
return <MediasfuGeneric credentials={credentials} />;
}
```
**When to use:**
- β
Prototyping or MVP development
- β
Need a production-ready UI quickly
- β
Standard video conferencing features are sufficient
##### Mode 2: Custom UI with MediaSFU Backend (Most Flexible)
Build your own UI while using MediaSFU's powerful backend infrastructure.
```javascript
import { MediasfuGeneric } from 'mediasfu-reactjs';
import { useState } from 'react';
function App() {
const [sourceParameters, setSourceParameters] = useState(null);
const handleUpdateSourceParameters = (params) => {
setSourceParameters(params);
};
return (
<div>
<MediasfuGeneric
returnUI={false}
sourceParameters={sourceParameters}
updateSourceParameters={handleUpdateSourceParameters}
credentials={credentials}
noUIPreJoinOptions={{
action: 'create',
userName: 'Your Name',
capacity: 50,
duration: 30,
eventType: 'conference'
}}
/>
{/* Your custom UI */}
{sourceParameters && (
<div className="custom-controls">
<button onClick={() => sourceParameters.clickVideo({ parameters: sourceParameters })}>
Toggle Video
</button>
<button onClick={() => sourceParameters.clickAudio({ parameters: sourceParameters })}>
Toggle Audio
</button>
</div>
)}
</div>
);
}
```
**When to use:**
- β
Need complete control over UI/UX
- β
Building a custom branded experience
- β
Integrating into existing app design
##### Mode 3: Component Replacement (Balanced)
Replace specific MediaSFU components while keeping the rest of the infrastructure.
```javascript
import { MediasfuGeneric, FlexibleVideo, FlexibleGrid } from 'mediasfu-reactjs';
function CustomMainScreen({ parameters }) {
return (
<div style={{ display: 'flex', flexDirection: 'column', height: '100vh' }}>
{/* Custom header */}
<div className="custom-header">
<h1>{parameters.roomName}</h1>
<span>{parameters.participants.length} participants</span>
</div>
{/* Use MediaSFU's components in your layout */}
<FlexibleVideo
customWidth={window.innerWidth}
customHeight={600}
parameters={parameters}
/>
<FlexibleGrid
customWidth={window.innerWidth}
customHeight={400}
parameters={parameters}
/>
{/* Custom footer */}
<div className="custom-footer">
<button onClick={() => parameters.clickVideo({ parameters })}>
{parameters.videoAlreadyOn ? 'Stop Video' : 'Start Video'}
</button>
</div>
</div>
);
}
function App() {
return (
<MediasfuGeneric
credentials={credentials}
PrejoinPage={WelcomePage}
customComponent={CustomMainScreen}
/>
);
}
```
**When to use:**
- β
Need custom main interface but want to keep MediaSFU's components
- β
Partial customization with minimal effort
- β
Want to maintain MediaSFU's functionality while customizing layout
#### 3. **Parameters: Your Control Center**
The `sourceParameters` object (or `parameters` in custom components) is your gateway to all MediaSFU functionality:
```javascript
// Available in sourceParameters or parameters object
{
// Media Controls
clickVideo: (options) => {},
clickAudio: (options) => {},
clickScreenShare: (options) => {},
// Room State
roomName: 'meeting-123',
participants: [...],
allVideoStreams: [...],
allAudioStreams: [...],
// UI State
videoAlreadyOn: false,
audioAlreadyOn: false,
screenAlreadyOn: false,
// Update Functions
updateVideoAlreadyOn: (value) => {},
updateAudioAlreadyOn: (value) => {},
// And 200+ more properties and methods...
}
```
**Access patterns:**
```javascript
// In Mode 1 (Default UI): Parameters are managed internally
// You don't need to access them directly
// In Mode 2 (Custom UI): Access via sourceParameters state
const [sourceParameters, setSourceParameters] = useState(null);
sourceParameters?.clickVideo({ parameters: sourceParameters });
// In Mode 3 (Component Replacement): Passed to your custom component
function CustomComponent({ parameters }) {
parameters.clickVideo({ parameters });
}
```
---
## Core Concepts & Components <a name="core-concepts--components"></a>
Now that you understand the architecture, let's explore the building blocks.
### 1. Display Components: Building Your Video Layout
MediaSFU provides powerful components for organizing and displaying media streams.
#### Primary Layout Components
**FlexibleVideo** - Main video display area
```javascript
import { FlexibleVideo } from 'mediasfu-reactjs';
<FlexibleVideo
customWidth={window.innerWidth}
customHeight={600}
parameters={parameters}
/>
```
- Automatically handles main presenter or screen share
- Smooth transitions between different video sources
- Responsive sizing
**FlexibleGrid** - Participant grid layout
```javascript
import { FlexibleGrid } from 'mediasfu-reactjs';
<FlexibleGrid
customWidth={window.innerWidth}
customHeight={800}
parameters={parameters}
/>
```
- Intelligent grid sizing (2x2, 3x3, 4x4, etc.)
- Pagination for large participant lists
- Automatic reflow on window resize
**AudioGrid** - Audio-only participants
```javascript
import { AudioGrid } from 'mediasfu-reactjs';
<AudioGrid parameters={parameters} />
```
- Displays participants without video
- Audio level indicators
- Compact layout for efficiency
#### Container Components
| Component | Purpose | Use Case |
|-----------|---------|----------|
| **MainContainerComponent** | Primary content wrapper | Wraps all main content areas |
| **MainAspectComponent** | Aspect ratio container | Maintains proper video proportions |
| **MainScreenComponent** | Screen layout manager | Organizes screen regions |
| **SubAspectComponent** | Secondary content container | For picture-in-picture, sidebars |
**Example: Building a custom layout**
```javascript
import {
MainContainerComponent,
FlexibleVideo,
FlexibleGrid,
AudioGrid
} from 'mediasfu-reactjs';
function CustomLayout({ parameters }) {
return (
<MainContainerComponent>
<div style={{ display: 'flex', flexDirection: 'column', height: '100vh' }}>
{/* Main video area */}
<div style={{ flex: 3 }}>
<FlexibleVideo
customWidth={window.innerWidth}
customHeight={window.innerHeight * 0.6}
parameters={parameters}
/>
</div>
{/* Participant grid */}
<div style={{ flex: 2 }}>
<FlexibleGrid
customWidth={window.innerWidth}
customHeight={window.innerHeight * 0.3}
parameters={parameters}
/>
</div>
{/* Audio-only participants */}
<div style={{ height: 80 }}>
<AudioGrid parameters={parameters} />
</div>
</div>
</MainContainerComponent>
);
}
```
### 2. Control Components: User Interactions
**ControlButtonsComponent** - Standard control bar
```javascript
import { ControlButtonsComponent } from 'mediasfu-reactjs';
<ControlButtonsComponent
parameters={parameters}
position="bottom" // or 'top', 'left', 'right'
/>
```
Includes: mute, video, screenshare, participants, chat, settings, etc.
**ControlButtonsAltComponent** - Alternative layout
```javascript
import { ControlButtonsAltComponent } from 'mediasfu-reactjs';
<ControlButtonsAltComponent
parameters={parameters}
position="top"
/>
```
Different button arrangement optimized for specific layouts.
**ControlButtonsComponentTouch** - Touch-optimized controls
```javascript
import { ControlButtonsComponentTouch } from 'mediasfu-reactjs';
<ControlButtonsComponentTouch parameters={parameters} />
```
Floating action buttons optimized for mobile/tablet interfaces.
### 3. Modal Components: Feature Interfaces
MediaSFU includes modals for various features:
```javascript
import {
ParticipantsModal,
MessagesModal,
SettingsModal,
DisplaySettingsModal,
RecordingModal,
PollModal,
BreakoutRoomsModal
} from 'mediasfu-reactjs';
// These are automatically rendered when enabled
// Control their visibility via parameters
parameters.updateIsParticipantsModalVisible(true);
parameters.updateIsMessagesModalVisible(true);
parameters.updateIsSettingsModalVisible(true);
```
Available modals:
- **ParticipantsModal** - Participant list management
- **MessagesModal** - Chat interface
- **SettingsModal** - Event and room settings
- **DisplaySettingsModal** - Layout and display options
- **RecordingModal** - Recording controls and settings
- **PollModal** - Create and manage polls
- **BreakoutRoomsModal** - Breakout room management
- **MediaSettingsModal** - Camera/microphone selection
- **BackgroundModal** - Virtual background settings
- **ConfigureWhiteboardModal** - Whiteboard configuration
**Example: Programmatically showing modals**
```javascript
function CustomControls({ parameters }) {
return (
<div className="custom-toolbar">
<button onClick={() => parameters.updateIsParticipantsModalVisible(true)}>
Show Participants ({parameters.participants.length})
</button>
<button onClick={() => parameters.updateIsMessagesModalVisible(true)}>
Open Chat
</button>
<button onClick={() => parameters.launchPoll.launchPoll({ parameters })}>
Create Poll
</button>
</div>
);
}
```
### 4. Video Cards: Individual Participant Display
**VideoCard** - Individual participant video element
```javascript
import { VideoCard } from 'mediasfu-reactjs';
<VideoCard
videoStream={participantStream}
remoteProducerId="producer-id"
eventType="conference"
forceFullDisplay={false}
participant={participantObject}
backgroundColor="#000000"
showControls={true}
showInfo={true}
name="Participant Name"
parameters={parameters}
/>
```
**AudioCard** - Individual audio-only participant
```javascript
import { AudioCard } from 'mediasfu-reactjs';
<AudioCard
name="Participant Name"
barColor="#4CAF50"
textColor="#FFFFFF"
customStyle={{ borderRadius: '10px' }}
controlsPosition="topLeft"
infoPosition="topRight"
participant={participantObject}
parameters={parameters}
/>
```
**MiniCard** - Compact participant display (for grids)
```javascript
import { MiniCard } from 'mediasfu-reactjs';
<MiniCard
participant={participantObject}
showControls={false}
parameters={parameters}
/>
```
**Example: Custom Video Card**
```javascript
function MyCustomVideoCard({ stream, participant, parameters }) {
return (
<div style={{
border: '3px solid #00ff88',
borderRadius: '15px',
overflow: 'hidden',
position: 'relative'
}}>
<video
ref={videoRef => {
if (videoRef && stream) {
videoRef.srcObject = stream;
videoRef.play();
}
}}
style={{ width: '100%', height: '100%', objectFit: 'cover' }}
autoPlay
muted
/>
<div style={{
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
background: 'rgba(0, 255, 136, 0.8)',
color: 'black',
padding: '8px',
fontWeight: 'bold'
}}>
{participant.name} {participant.muted && 'π'}
</div>
</div>
);
}
// Use it
<MediasfuGeneric
credentials={credentials}
customVideoCard={MyCustomVideoCard}
/>
```
---
## Working with Methods <a name="working-with-methods"></a>
MediaSFU provides 200+ methods for controlling every aspect of your real-time communication experience. Let's explore the most important categories.
### Media Control Methods
#### Video Control
```javascript
// Toggle video on/off
parameters.clickVideo({ parameters });
// Switch camera (front/back on mobile)
parameters.switchVideoAlt({ parameters });
// Switch to specific camera by ID
const cameras = await parameters.getMediaDevicesList('videoinput');
parameters.switchUserVideo({
videoPreference: cameras[1].deviceId,
parameters
});
// Get current video state
const isVideoOn = parameters.videoAlreadyOn;
// Update video state programmatically
parameters.updateVideoAlreadyOn(true);
```
#### Audio Control
```javascript
// Toggle audio on/off
parameters.clickAudio({ parameters });
// Switch microphone
const microphones = await parameters.getMediaDevicesList('audioinput');
parameters.switchUserAudio({
audioPreference: microphones[1].deviceId,
parameters
});
// Get current audio state
const isAudioOn = parameters.audioAlreadyOn;
const hasHostPermission = parameters.micAction; // Host approval status
// Mute/unmute specific participant (host only)
parameters.controlMedia({
participantId: 'participant-id',
participantName: 'John Doe',
type: 'audio',
socket: parameters.socket,
roomName: parameters.roomName
});
```
#### Screen Sharing
```javascript
// Start screen sharing
parameters.clickScreenShare({ parameters });
// Stop screen sharing
parameters.stopShareScreen({ parameters });
// Check if screen sharing is available
const canShare = await parameters.checkScreenShare({ parameters });
// Get screen share state
const isSharing = parameters.screenAlreadyOn;
const shareAudio = parameters.shareScreenStarted; // Sharing with audio
```
### Device Management Methods
```javascript
// Get available cameras
const cameras = await parameters.getMediaDevicesList('videoinput');
cameras.forEach(camera => {
console.log(`Camera: ${camera.label} (${camera.deviceId})`);
});
// Get available microphones
const microphones = await parameters.getMediaDevicesList('audioinput');
microphones.forEach(mic => {
console.log(`Microphone: ${mic.label} (${mic.deviceId})`);
});
// Building a device selector UI
function DeviceSelector({ parameters }) {
const [cameras, setCameras] = useState([]);
const [microphones, setMicrophones] = useState([]);
useEffect(() => {
const loadDevices = async () => {
const cams = await parameters.getMediaDevicesList('videoinput');
const mics = await parameters.getMediaDevicesList('audioinput');
setCameras(cams);
setMicrophones(mics);
};
loadDevices();
}, []);
return (
<div>
<select onChange={(e) => {
parameters.switchUserVideo({
videoPreference: e.target.value,
parameters
});
}}>
{cameras.map(camera => (
<option key={camera.deviceId} value={camera.deviceId}>
{camera.label}
</option>
))}
</select>
<select onChange={(e) => {
parameters.switchUserAudio({
audioPreference: e.target.value,
parameters
});
}}>
{microphones.map(mic => (
<option key={mic.deviceId} value={mic.deviceId}>
{mic.label}
</option>
))}
</select>
</div>
);
}
```
### Participant Management Methods
```javascript
// Get all participants
const participants = parameters.participants;
const participantCount = parameters.participantsCounter;
// Filter participants
const videoParticipants = participants.filter(p => p.videoOn);
const audioOnlyParticipants = participants.filter(p => !p.videoOn);
const mutedParticipants = participants.filter(p => p.muted);
// Find specific participant
const participant = participants.find(p => p.name === 'John Doe');
// Remove participant from room (host only)
parameters.disconnectUserInitiate({
member: participantId,
roomName: parameters.roomName,
socket: parameters.socket
});
// Change participant role (host only)
parameters.updateParticipant({
participantId: 'participant-id',
islevel: '2', // '2' = host, '1' = co-host, '0' = participant
parameters
});
// Request to unmute participant (sends request)
parameters.requestScreenShare({ parameters });
```
### Chat & Messaging Methods
```javascript
// Send a group message
parameters.sendMessage({
message: 'Hello everyone!',
type: 'group',
parameters
});
// Send direct message
parameters.sendMessage({
message: 'Private message',
type: 'direct',
receivers: ['participant-id'],
parameters
});
// Access message history
const messages = parameters.messages;
// Listen for new messages (via update function)
parameters.updateMessages = (newMessages) => {
console.log('New messages:', newMessages);
};
// Example: Custom chat component
function CustomChat({ parameters }) {
const [message, setMessage] = useState('');
const sendMessage = () => {
parameters.sendMessage({
message,
type: 'group',
parameters
});
setMessage('');
};
return (
<div>
<div className="messages">
{parameters.messages.map((msg, index) => (
<div key={index}>
<strong>{msg.sender}:</strong> {msg.message}
</div>
))}
</div>
<input
value={message}
onChange={(e) => setMessage(e.target.value)}
onKeyPress={(e) => e.key === 'Enter' && sendMessage()}
/>
<button onClick={sendMessage}>Send</button>
</div>
);
}
```
### Recording Methods
```javascript
// Start recording
parameters.startRecording({ parameters });
// Stop recording
parameters.stopRecording({ parameters });
// Pause recording
parameters.pauseRecording({ parameters });
// Resume recording
parameters.resumeRecording({ parameters });
// Configure recording settings
parameters.updateRecording({
recordingMediaOptions: 'video', // or 'audio'
recordingAudioOptions: 'all', // or 'host'
recordingVideoOptions: 'all', // or 'host'
recordingVideoType: 'fullDisplay', // or 'bestDisplay', 'all'
recordingDisplayType: 'video', // 'media', 'video', 'all'
recordingBackgroundColor: '#000000',
recordingNameTagsColor: '#ffffff',
recordingOrientationVideo: 'landscape', // or 'portrait'
recordingNameTags: true,
recordingAddHLS: false,
parameters
});
// Check recording state
const isRecording = parameters.recordStarted;
const isPaused = parameters.recordPaused;
const recordingTime = parameters.recordElapsedTime;
```
### Polls & Surveys Methods
```javascript
// Create a poll
parameters.handleCreatePoll({
poll: {
question: 'What time works best?',
type: 'multiple', // or 'single'
options: ['10 AM', '2 PM', '5 PM']
},
parameters
});
// Vote on a poll
parameters.handleVotePoll({
pollId: 'poll-id',
optionIndex: 1,
parameters
});
// End a poll
parameters.handleEndPoll({
pollId: 'poll-id',
parameters
});
// Access poll data
const polls = parameters.polls;
const activePoll = polls.find(p => p.status === 'active');
// Example: Custom poll component
function CustomPoll({ parameters }) {
const activePoll = parameters.polls.find(p => p.status === 'active');
if (!activePoll) return null;
return (
<div className="poll">
<h3>{activePoll.question}</h3>
{activePoll.options.map((option, index) => (
<button
key={index}
onClick={() => {
parameters.handleVotePoll({
pollId: activePoll.id,
optionIndex: index,
parameters
});
}}
>
{option} ({activePoll.votes?.[index] || 0} votes)
</button>
))}
</div>
);
}
```
### Breakout Rooms Methods
```javascript
// Create breakout rooms
parameters.createBreakoutRooms({
numberOfRooms: 3,
participants: parameters.participants,
parameters
});
// Assign participant to room
parameters.assignParticipantToRoom({
participantId: 'participant-id',
roomIndex: 0,
parameters
});
// Start breakout rooms
par