useRoom

The useRoom hook exposes primitives to interact with a Huddle01 room, like joining, leaving, closing the room.

state can be idle, connecting, connected, failed, left, or closed

NameDescriptionReturn TypeParams
roomThe currently joined room object.Room
stateThe state of the currently joined room.RoomStates
joinRoomJoin the room.Promise<Room>{ roomId: string; token: string }
leaveRoomLeave the room.void
closeRoomClose the room.void
kickPeerRemove the peer.voidstring
muteEveryoneMute Everyonevoid

Example Usage

import { useRoom } from '@huddle01/react/hooks';
 
const {
    room,
    state,
    joinRoom,
    leaveRoom,
    closeRoom,
    kickPeer,
    muteEveryone,
    closeStreamOfLabel,
  } = useRoom({
  onJoin(data:Room) ();
  onWaiting(data: {
    reason: 'WAITING_FOR_PERMISSIONS' | 'WAITING_FOR_ROOM_TO_START' | 'WAITING_FOR_ADMIN_TO_JOIN';
    message?: string;
  }) ();
  onLeave(data: {
    reason: 'LEFT' | 'CLOSED' | 'KICKED' | 'DENIED';
    message?: string;
  }) ();
  onFailed(data: {
      status: 'ROOM_NOT_FOUND' | 'ROOM_ERRORED';
      message: string;
  }) ();
  onPeerJoin(peerId: string) ();
  onPeerLeft(peerId: string) ();
  });
 
// Join room
const join = async () => {
  await joinRoom({
    roomId,
    tokenId
  })
}
 
// Leave room
const leave = async () => {
  await leaveRoom()
}
 
// Close room
const close = async () => {
  await closeRoom()
}
 
// Kick peer
const kick = async () => {
  await kickPeer(peerId)
}
 
// Mute everyone
const mute = async () => {
  await muteEveryone()
}

Props

The useRoom hook has following props which you can use to handle various events.

onJoin

Optional
DescriptionReturn Type
This function will be called when you successfully join a room.void
Parameter NameTypeDescriptionRequired
data{ room: Room }Object containing data about the room.Yes

Example Usage

const room = useRoom({ onJoin: (data) => {
  console.log("Successfully joined the room!");
  console.log(data.room);
    // your code here
}});

onWaiting

Optional
DescriptionReturn Type
This function will be called when you are attempting to join the room.void
Parameter NameTypeDescriptionRequired
data{reason:'WAITING_FOR_PERMISSIONS' | 'WAITING_FOR_ROOM_TO_START' | 'WAITING_FOR_ADMIN_TO_JOIN', message?: string}Object containing reasons.Yes

Example Usage

const room = useRoom({ onWaiting: (data) => {
  console.log("Waiting for the host to admit you");
  console.log(data.reason);
    // your code here
}});

onLeave

Optional
DescriptionReturn Type
This function will be called when you successfully leave the room.void
Parameter NameTypeDescriptionRequired
data{reason:'LEFT' | 'CLOSED' | 'KICKED' | 'DENIED', message?: string}Object containing reasons.Yes

Example Usage

const room = useRoom({ onLeave: (data) => {
  console.log("Successfully leave the room!");
    // your code here
}});

onFailed

Optional
DescriptionReturn Type
This function will be called when you are not able to join the room.void
Parameter NameTypeDescriptionRequired
data{status:'ROOM_NOT_FOUND' | 'ROOM_ERRORED', message: string}Object containing reasons.Yes

Example Usage

const room = useRoom({ onFailed: (data) => {
  console.log("The room was failed!");
    // your code here
}});

onPeerJoin

Optional
DescriptionReturn Type
This function will be called when a peer successfully joins the room.void
Parameter NameTypeRequired
peerIdstringYes

Example Usage

const room = useRoom({ onPeerJoin: (peerId:string) => {
  console.log(`${peerId} has joined the room.`);
    // your code here
}});

onPeerLeft

Optional
DescriptionReturn Type
This function will be called when a peer successfully left the room.void
Parameter NameTypeRequired
peerIdstringYes

Example Usage

const room = useRoom({ onPeerLeft: (peerId:string) => {
  console.log(`${peerId} has left the room.`);
    // your code here
}});
Audio/Video Infrastructure designed for the developers to empower them ship simple yet powerful Audio/Video Apps.
support
company
Copyright © 2024 Graphene 01, Inc. All Rights Reserved.