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
Name | Description | Return Type | Params |
---|---|---|---|
room | The currently joined room object. | Room | |
state | The state of the currently joined room. | RoomStates | |
joinRoom | Join the room. | Promise<Room> | { roomId: string; token: string } |
leaveRoom | Leave the room. | void | |
closeRoom | Close the room. | void | |
kickPeer | Remove the peer. | void | string |
muteEveryone | Mute Everyone | void |
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
Description | Return Type |
---|---|
This function will be called when you successfully join a room. | void |
Parameter Name | Type | Description | Required |
---|---|---|---|
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
Description | Return Type |
---|---|
This function will be called when you are attempting to join the room. | void |
Parameter Name | Type | Description | Required |
---|---|---|---|
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
Description | Return Type |
---|---|
This function will be called when you successfully leave the room. | void |
Parameter Name | Type | Description | Required |
---|---|---|---|
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
Description | Return Type |
---|---|
This function will be called when you are not able to join the room. | void |
Parameter Name | Type | Description | Required |
---|---|---|---|
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
Description | Return Type |
---|---|
This function will be called when a peer successfully joins the room. | void |
Parameter Name | Type | Required |
---|---|---|
peerId | string | Yes |
Example Usage
const room = useRoom({ onPeerJoin: (peerId:string) => {
console.log(`${peerId} has joined the room.`);
// your code here
}});
onPeerLeft
Optional
Description | Return Type |
---|---|
This function will be called when a peer successfully left the room. | void |
Parameter Name | Type | Required |
---|---|---|
peerId | string | Yes |
Example Usage
const room = useRoom({ onPeerLeft: (peerId:string) => {
console.log(`${peerId} has left the room.`);
// your code here
}});