Docs

User roles and permissions

Ask for the microphone and camera the right way, give each user a role from your server, and move users between listener and speaker. Flutter and React Native.

1. Microphone and camera

Declare the permissions once (Android manifest, iOS Info.plist, see platform setup and React Native setup), and ask the user at runtime before they join with the microphone or camera on.

lib/permissions.dart
// pubspec.yaml: permission_handler (or your own permission flow)
import 'package:permission_handler/permission_handler.dart';

Future<bool> askMicAndCamera() async {
  final r = await [Permission.microphone, Permission.camera].request();
  return r.values.every((s) => s.isGranted);
}

// Join with what the user allowed; a refused user can still join as a listener.
final ok = await askMicAndCamera();
await room.connect(url: url, token: token, publishMic: ok, enableVideo: ok);
permissions.ts
import { PermissionsAndroid, Platform } from 'react-native';

export async function askMicAndCamera(): Promise<boolean> {
  if (Platform.OS !== 'android') return true; // iOS asks on first use (Info.plist strings)
  const r = await PermissionsAndroid.requestMultiple([
    PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
    PermissionsAndroid.PERMISSIONS.CAMERA,
  ]);
  return Object.values(r).every((s) => s === PermissionsAndroid.RESULTS.GRANTED);
}
  • Ask at the moment it makes sense, for example on the "Go live" or "Take a seat" button, not at app start.
  • A user who refuses can still join as a listener: join without the microphone and camera.
  • iOS: NSMicrophoneUsageDescription and NSCameraUsageDescription must be in Info.plist, or the app is closed when the room opens the microphone.

2. Roles come from the token

What a user may do in a room is set by the token your server signs, not by the app. The app can ask for a role; your token endpoint decides, from your own login session and your seat rules.

RoleToken optionsWhat they can do
HostcanPublish: trueCamera, microphone and screen
Speaker (seat)canPublish: true, canPublishSources: ['microphone']Voice only
Listener / viewercanPublish: falseHears and sees, sends nothing
Hidden (recorder, moderator)hidden: trueNot listed to other participants
server/token.js
import { createToken } from 'gravix-rtc-token';

// Your endpoint decides the role from YOUR session and seat rules, never from the request alone.
function tokenFor(user, roomId, role) {
  return createToken(APP_ID, APP_CERTIFICATE, {
    room: roomId,
    identity: user.id,
    name: user.name,
    canPublish: role !== 'listener',                       // listener: hears and sees, cannot send
    canPublishSources: role === 'speaker' ? ['microphone'] : undefined, // speaker: voice only
  });
}

All options, also for Go and Java: token options.

3. Bring a listener on stage

A listener who is invited to a seat needs a new token with publishing rights. Your server signs it, and the app rejoins the same room with it. On Flutter it took about a second in our test; the rest of the room keeps going.

lib/seat.dart
// The host invited this listener to a seat; your server agreed and signs a speaker token.
final c = await tokens.getCredentials(
    GravixTokenRequest(room: roomId, identity: userId, canPublish: true));
await room.reconnectWithToken(c.token, publishMic: true);   // about a second in our test; same room
seat.ts
// The host invited this listener to a seat; your server agreed and signs a speaker token.
const { url, token } = await tokens.getCredentials({ room: roomId, identity: userId, canPublish: true });
await room.disconnect();
const speaker = new Room({ adaptiveStream: true, dynacast: true });
await speaker.connect(url, token);                         // the same room, now with publish rights
await speaker.localParticipant.setMicrophoneEnabled(true);
// The audio session started at the first join keeps running.
  • To move a speaker back to the audience, turn their microphone off and stop giving them publishing tokens; with the next rejoin they are a listener again.
  • With the beauty filter, Flutter's reconnectWithToken(token, publishMic: true, enableVideo: true) brings the filtered camera too. In React Native, rejoin with a GravixRoomCamera as in the beauty filter guide.

4. Seats without a rejoin

Your server can also change a user's rights while they stay in the room, with theroom API. The user keeps their connection; nothing else in the room moves. In our test a listener was given a seat, spoke, lost the seat and got it back, all in one session; the other users heard the microphone about a third of a second after the permission arrived.

server/seats.js
import { createToken, roomName } from 'gravix-rtc-token';

const APP_ID = process.env.GRAVIX_APP_ID;
const APP_CERT = process.env.GRAVIX_APP_CERTIFICATE;

// Any Gravix region host works; a call for a user in another region is forwarded there.
async function roomApi(host, method, room, body) {
  const token = await createToken(APP_ID, APP_CERT, {
    room, identity: 'my-backend', roomAdmin: true, canPublish: false, ttlSeconds: 300,
  });
  const res = await fetch(`https://${host}/twirp/gravix.RoomService/${method}`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
    body: JSON.stringify({ room: roomName(APP_ID, room), ...body }),
  });
  if (!res.ok) throw new Error(`${method}: ${res.status} ${await res.text()}`);
  return res.json();
}

// Give a listener a seat, without a rejoin:
await roomApi('blr1.gravixcloud.com', 'UpdateParticipant', 'party-42', {
  identity: 'user-123',
  permission: { canSubscribe: true, canPublish: true, canPublishData: true },
});

// Take the seat back: their microphone is unpublished, they stay in the room as a listener.
await roomApi('blr1.gravixcloud.com', 'UpdateParticipant', 'party-42', {
  identity: 'user-123',
  permission: { canSubscribe: true, canPublish: false, canPublishData: true },
});

In the app, turn the microphone on when the permission arrives:

seat.ts
import { RoomEvent } from 'gravix-rtc-react-native';

// The server gave this user a seat (or took it): turn the microphone on when allowed.
room.on(RoomEvent.ParticipantPermissionsChanged, async (_prev, participant) => {
  if (participant !== room.localParticipant) return;
  if (room.localParticipant.permissions?.canPublish) {
    await room.localParticipant.setMicrophoneEnabled(true);
  }
  // When the seat is taken away, the server unpublishes the microphone itself.
});
  • Any Gravix region host works: the call is forwarded to the region the user is connected to. A user who has left answers 404.
  • For a party room with many seats this avoids a rejoin per seat change. The token-and-rejoin way above keeps working.

5. Host actions

Seats, invites, bans and who is the host are your app's rules, kept on your server. Your server enforces them through the tokens it signs: no new token for a banned user, a listener token for someone who loses a seat.

From your server, the room API also mutes a user's track (MutePublishedTrack) and removes a user from a live room (RemoveParticipant). A removed user can join again only with a new token, so stop signing tokens for them first.

Questions about roles in your app?

Call or WhatsApp us, or sign up free and try it with your own rooms.