A view mode decides which remote participants your app receives audio and video from.
A meeting can have many participants. All of them do not fit on one screen. Also, video from every participant uses a lot of network and device resources. For this reason, the Core SDK receives media from some participants only. The view mode decides which participants the SDK selects.
RealtimeKit supports three view modes:
| View mode | Who selects the participants | How it works |
|---|---|---|
ACTIVE_GRID |
The SDK | The SDK selects the participants. It checks who is speaking. This is the default. |
PAGINATED |
Your app, one page at a time | The SDK divides the participants into pages. Your app chooses the page. |
MANUAL |
Your app, one participant at a time | Your app chooses each participant and each kind of media. The SDK does not select any participant. |
The meeting.participants.joined map contains every remote participant in the meeting. But your app does not receive media from every participant in this map.
The SDK has two more maps for the selected participants:
meeting.participants.videoSubscribedcontains the participants whose video your app receives. Use this map to show your video grid.meeting.participants.activeis another name for the same map.meeting.participants.audioSubscribedcontains the participants whose audio your app receives.
The view mode decides which participants are in these two maps. When the selected participants change, the SDK updates the maps. The SDK also starts or stops receiving media from these participants.
The following table shows which participants are in the maps in each view mode:
| View mode | videoSubscribed |
audioSubscribed |
|---|---|---|
ACTIVE_GRID |
Recent speakers and pinned participants first. Then other participants, until the grid is full. | Recent speakers. This map can have a few more participants than the video grid. |
PAGINATED |
Participants on the current page, and pinned participants | Recent speakers from all pages |
MANUAL |
Participants that your app subscribes to | Participants that your app subscribes to |
The local participant is never in these maps. To show the local participant, use meeting.self.
The grid size is the maximum number of participants whose video the SDK receives at one time. In PAGINATED mode, the grid size is also the number of participants on one page.
The local participant's preset sets the grid size. To read it, use meeting.participants.maxActiveParticipantsCount. To change it, call meeting.participants.setMaxActiveParticipantsCount().
Use ACTIVE_GRID when you want the SDK to select the participants. This is the default mode. It works well for most meetings.
The SDK checks who is speaking. When a participant outside the grid starts to speak, the SDK adds this participant to the grid. To make space, the SDK removes a participant who is not speaking. Your app only needs to show the participants in meeting.participants.videoSubscribed.
Use PAGINATED when the meeting has more participants than the grid size, and you want users to see all participants page by page.
The SDK divides the participants into pages. The first page is page 1. Your app chooses the page. The SDK receives video only from the participants on that page. A participant does not move to another page when the participant speaks. Your app still receives audio from recent speakers on other pages, so users can hear them.
Use MANUAL when your app must choose each participant that it receives media from. For example, your app has its own layout rules. Or your app must receive only audio from some participants.
In this mode, the SDK does not select any participant. Your app calls SDK methods to start and stop receiving media from each participant. Your app must also handle the events when participants join, leave, or start to speak.
By default, a meeting starts in ACTIVE_GRID mode. To start in another view mode, pass viewMode in the defaults option when you initialize the SDK.
import { useEffect } from "react";
import { useRealtimeKitClient } from "@cloudflare/realtimekit-react";
export default function App() {
const [meeting, initMeeting] = useRealtimeKitClient();
useEffect(() => {
initMeeting({
authToken: "<auth-token>",
defaults: {
viewMode: "PAGINATED",
},
});
}, []);
return <div>Your meeting component comes here</div>;
}const meeting = await RealtimeKitClient.init({
authToken: "<auth-token>",
defaults: {
viewMode: "PAGINATED",
},
});import { Component, OnInit } from "@angular/core";
import RealtimeKitClient from "@cloudflare/realtimekit";
@Component({
selector: "app-meeting",
template: `<div>Your meeting component comes here</div>`,
})
export class MeetingComponent implements OnInit {
meeting: RealtimeKitClient;
async ngOnInit() {
this.meeting = await RealtimeKitClient.init({
authToken: "<auth-token>",
defaults: {
viewMode: "PAGINATED",
},
});
}
}This value is only the first view mode. Your app can change the view mode at any time after initialization.
If the value is not one of the three view modes, initialization fails with error code 1207.
To change the view mode, call meeting.participants.setViewMode() with the new view mode.
import { useRealtimeKitMeeting } from "@cloudflare/realtimekit-react";
function ViewModeButtons() {
const { meeting } = useRealtimeKitMeeting();
return (
<>
<button onClick={() => meeting.participants.setViewMode("ACTIVE_GRID")}>
Active grid
</button>
<button onClick={() => meeting.participants.setViewMode("PAGINATED")}>
Pages
</button>
</>
);
}// Let the SDK select participants
await meeting.participants.setViewMode("ACTIVE_GRID");
// Show participants in pages
await meeting.participants.setViewMode("PAGINATED");
// Select participants yourself
await meeting.participants.setViewMode("MANUAL");When you change to PAGINATED, the SDK shows page 1. If the meeting already uses the view mode that you pass, nothing changes.
To read the view mode and the page values, use the useRealtimeKitSelector hook. Your component renders again when a value changes.
import { useRealtimeKitSelector } from "@cloudflare/realtimekit-react";
function ViewModeLabel() {
const viewMode = useRealtimeKitSelector((m) => m.participants.viewMode);
const currentPage = useRealtimeKitSelector((m) => m.participants.currentPage);
const pageCount = useRealtimeKitSelector((m) => m.participants.pageCount);
if (viewMode !== "PAGINATED") {
return <span>{viewMode}</span>;
}
return (
<span>
Page {currentPage} of {pageCount}
</span>
);
}To read the view mode and the page values, use the following properties:
// "ACTIVE_GRID", "PAGINATED", or "MANUAL"
const viewMode = meeting.participants.viewMode;
// Current page in PAGINATED mode. 0 in the other modes.
const currentPage = meeting.participants.currentPage;
// Number of pages in PAGINATED mode. 0 in the other modes.
const pageCount = meeting.participants.pageCount;In PAGINATED mode, call meeting.participants.setPage() to choose the page. Pass a page number from 1 to meeting.participants.pageCount.
import {
useRealtimeKitMeeting,
useRealtimeKitSelector,
} from "@cloudflare/realtimekit-react";
function PageButtons() {
const { meeting } = useRealtimeKitMeeting();
const currentPage = useRealtimeKitSelector((m) => m.participants.currentPage);
const pageCount = useRealtimeKitSelector((m) => m.participants.pageCount);
return (
<>
<button
disabled={currentPage <= 1}
onClick={() => meeting.participants.setPage(currentPage - 1)}
>
Previous
</button>
<button
disabled={currentPage >= pageCount}
onClick={() => meeting.participants.setPage(currentPage + 1)}
>
Next
</button>
</>
);
}const { currentPage, pageCount } = meeting.participants;
// Go to the next page
if (currentPage < pageCount) {
await meeting.participants.setPage(currentPage + 1);
}
// Go to the previous page
if (currentPage > 1) {
await meeting.participants.setPage(currentPage - 1);
}Note the following:
setPage()has no effect inACTIVE_GRIDandMANUALmodes.- The number of pages changes when participants join or leave. Read
pageCountagain before you show your page controls. - When participants leave, the current page can become empty and disappear. Then the SDK moves to the last page. The view mode is still
PAGINATED.
In MANUAL mode, your app selects participants with two methods:
meeting.participants.subscribe(peerIds, kinds)starts receiving media from the participants.meeting.participants.unsubscribe(peerIds, kinds)stops receiving media from the participants.
Both methods have two arguments: a list of peer IDs, and an optional list of media kinds.
The peer ID is the id field of a participant in meeting.participants.joined. Do not use the user ID or the custom participant ID of the participant.
The following table lists the media kinds:
| Kind | Media |
|---|---|
audio |
Microphone audio |
video |
Camera video |
screenshareAudio |
Audio of a shared screen |
screenshareVideo |
Video of a shared screen |
If you do not pass the media kinds, the method uses all four kinds.
The following example shows buttons for one participant. Pass the participant's id as peerId.
import { useRealtimeKitMeeting } from "@cloudflare/realtimekit-react";
// peerId is the id field of a participant in meeting.participants.joined
function SubscribeButtons({ peerId }: { peerId: string }) {
const { meeting } = useRealtimeKitMeeting();
const receiveEverything = () => meeting.participants.subscribe([peerId]);
const receiveAudioOnly = async () => {
await meeting.participants.subscribe([peerId], ["audio"]);
await meeting.participants.unsubscribe(
[peerId],
["video", "screenshareVideo"],
);
};
const receiveNothing = () => meeting.participants.unsubscribe([peerId]);
return (
<>
<button onClick={receiveEverything}>Audio and video</button>
<button onClick={receiveAudioOnly}>Audio only</button>
<button onClick={receiveNothing}>Nothing</button>
</>
);
}For a complete app that uses manual mode, refer to the React manual subscription example ↗︎.
The following example takes three participants from meeting.participants.joined and passes the id of each one as the peer ID.
await meeting.participants.setViewMode("MANUAL");
// Each participant in the joined map has an id field. This is the peer ID.
const [first, second, third] = meeting.participants.joined.toArray();
// Receive all media from two participants
await meeting.participants.subscribe([first.id, second.id]);
// Receive only microphone audio from a third participant
await meeting.participants.subscribe([third.id], ["audio"]);
// Stop receiving camera video from the first participant
await meeting.participants.unsubscribe([first.id], ["video"]);
// Stop receiving all media from the second participant
await meeting.participants.unsubscribe([second.id]);For a complete app that uses manual mode, refer to the Web Components manual subscription example ↗︎.
For a complete app that uses manual mode, refer to the Angular manual subscription example ↗︎.
These methods update meeting.participants.videoSubscribed and meeting.participants.audioSubscribed. Then the SDK starts or stops receiving the media. Use these maps to show your grid, as in the other view modes.
Note the following:
- Both methods work only in
MANUALmode. In the other modes, the methods fail with error code1206. - When you change to
MANUALduring a meeting, the SDK keeps the participants that are selected at that time. After that, the SDK removes a participant only when the participant leaves the meeting. - The SDK does not select participants who join later. Listen for new participants on
meeting.participants.joined. Then subscribe to the participants that you want. - A participant is in
videoSubscribedwhile your app receives the participant's camera video or screen share video. In the same way, a participant is inaudioSubscribedwhile your app receives the participant's microphone audio or screen share audio.
The SDK emits events when the view mode, the page, or the selected participants change. Use these events to update your UI.
The following table lists the events that relate to view modes:
| Event | Emitted on | When |
|---|---|---|
viewModeChanged |
meeting.participants |
The view mode changes. |
pageChanged |
meeting.participants |
The page changes in PAGINATED mode. |
participantJoined |
meeting.participants.videoSubscribed and meeting.participants.audioSubscribed |
A participant is added to the map. |
participantLeft |
meeting.participants.videoSubscribed and meeting.participants.audioSubscribed |
A participant is removed from the map. |
videoUpdate |
Each participant, and each map that contains the participant | The participant's camera video turns on or off. |
audioUpdate |
Each participant, and each map that contains the participant | The participant's microphone audio turns on or off. |
screenShareUpdate |
Each participant, and each map that contains the participant | The participant's screen share starts or stops. |
The SDK does not emit viewModeChanged for the view mode that you set during initialization.
For code examples of each event, refer to Remote participant events.
When the connection is lost and then restored, the SDK keeps the current view mode. The SDK does not return to the initial view mode.
In PAGINATED mode, the SDK also keeps the current page. If this page does not exist after the reconnection, the SDK moves to the last page.
The UI Kit components read the view mode from the Core SDK and update automatically. For the UI Kit components that show and change the view mode, refer to UI Kit view modes.