useRoom
The useRoom
hook provides access to room functionalities, such as joining, leaving, and managing participants.
Usage
Example:
const { room, joinRoom, leaveRoom, addComponent } = useRoom({
onMyParticipantJoined: (participant) => console.log('Component: My participant joined', participant),
onMyParticipantLeft: (participant) => console.log("Component: My participant left", participant),
});
Methods
joinRoom
JoinRoom is the main method to join a room. Without it, you won't be able to join a room. It allows you to join a room with a specific roomId, participant, group, and debug options.
Example:
const { joinRoom } = useRoom();
const handleJoin = async () => {
await joinRoom({
roomId: `ROOM_ID`,
participant: {
id: PARTICIPANT_ID,
name: PARTICIPANT_NAME,
email: PARTICIPANT_EMAIL,
},
group: {
name: GROUP_NAME,
id: GROUP_ID,
},
});
};
return <button onClick={handleJoin}>Join Room</button>;
Parameters:
Name | Type | Description |
---|---|---|
roomId | string | Required. This is a unique ID that you can create on your end. Using the same ID you will be able to enter the same room. Minimum length of 2 characters and maximum of 64. Accepted pattern: [-_&@+=,()\{\}\\[\\]\/«».:|'"#a-zA-Z0-9À-ÿ\s]* |
group | Group | Required. You can organize your participants into groups. Usually, a group is a company or organization. |
participant | Participant | Required. An object describing the user that will participate in the room. |
debug | boolean | Enable SDK logs in the browser console. Default value: false |
leaveRoom
Call this method to leave the room.
Example:
const { leaveRoom } = useRoom();
return <button onClick={leaveRoom}>Leave Room</button>;
getParticipants
Retrieves the list of participants in the room.
Example:
const { getParticipants } = useRoom();
getParticipants().then((participants) => {
console.log("Participants:", participants);
});
Returns: Promise<Participant[]>
On the callback function, you will receive the following argument:
Type | Description |
---|---|
Participant[] | Array of participants in the room |
Example:
[
{
"id": "USER_ID_1",
"email": "USER_EMAIL",
"name": "USER_NAME",
"activeComponents": ['videoConference'],
"type": "host",
"avatar": {
"imageUrl": "https://<PATH>",
}
},
{
"id": "USER_ID_2",
"email": "USER_EMAIL",
"name": "USER_NAME",
"activeComponents": ['videoConference'],
"type": "host",
"avatar": {
"imageUrl": "https://<PATH>",
}
},
];
addComponent
The addComponent method allows you to add a component such as video conference to the room. You can add multiple components to the same room including components from SuperViz collaboration.
Example:
import { RoomProvider, useRoom, addComponent } from "@superviz/react";
import { VideoConference } from "@superviz/video";
export const Children = () => {
const { joinRoom, addComponent } = useRoom({
onMyParticipantJoined: () => addVideo(),
});
const handleJoin = async () => {
await joinRoom({
roomId: `ROOM_ID`,
participant: {
id: PARTICIPANT_ID,
name: PARTICIPANT_NAME,
email: PARTICIPANT_EMAIL,
},
group: {
name: GROUP_NAME,
id: GROUP_ID,
},
});
const video = new VideoConference({
participantType: 'host'
});
addComponent(video);
};
return <button onClick={handleJoin}>Join Room and add video</button>;
};
export function SupervizReactRoom() {
return (
<RoomProvider developerToken={SUPERVIZ_KEY}>
<Children />
</RoomProvider>
);
}
Parameters:
Type | Description |
---|---|
component | Required. Pass the component instance you want to add to the room. |
removeComponent
The removeComponent method allows you to remove a component from the room.
Example:
import { RoomProvider, useRoom, removeComponent } from "@superviz/react";
import { VideoConference } from "@superviz/video";
export const Children = () => {
const { room, joinRoom, leaveRoom, addComponent, removeComponent } = useRoom({});
const video = useRef<VideoConference | null>(null);
const handleJoin = async () => {
await joinRoom({
participant: {
id: uuid,
name: "Participant Name",
email: 'carlos@superviz.com',
avatar: {
model3DUrl: 'https://production.storage.superviz.com/readyplayerme/1.glb',
imageUrl: 'https://production.cdn.superviz.com/static/default-avatars/1.png',
}
},
group: {
name: SUPERVIZ_ROOM_PREFIX,
id: SUPERVIZ_ROOM_PREFIX,
},
roomId: `${SUPERVIZ_ROOM_PREFIX}-${componentName}`,
debug: true,
environment: 'dev',
});
video.current = new VideoConference({
participantType: 'host'
});
addComponent(video.current);
};
const handleLeave = () => {
removeComponent(video.current);
leaveRoom();
};
useEffect(() => {
handleJoin();
return () => handleLeave();
}, [])
return (
<div>
<button disabled={!!room} onClick={handleJoin}>Join Room and add video</button>
<br />
<button disabled={!room} onClick={handleLeave}>Leave Room and remove video</button>
</div>
);
};
Callbacks
onMyParticipantJoined
Triggered when the local participant enters the room.
Example:
const { joinRoom } = useRoom({
onMyParticipantJoined: (participant) =>
console.log("Component: My participant joined", participant),
});
On the callback function, you will receive the following argument:
Type | Description |
---|---|
Participant | Details of the local participant who joined the room. |
onMyParticipantLeft
Triggered when the local participant leaves the room.
Example:
const { joinRoom } = useRoom({
onMyParticipantLeft: (participant) =>
console.log("Component: My participant left", participant),
});
On the callback function, you will receive the following argument:
Type | Description |
---|---|
Participant | Details of the local participant that left the room. |
onMyParticipantUpdated
Triggered when the local participant updates.
Example:
const { joinRoom } = useRoom({
onMyParticipantUpdated: (participant) =>
console.log("Component: My participant updated", participant),
});
On the callback function, you will receive the following argument:
Type | Description |
---|---|
Participant | Details of the local participant that was updated. |
onParticipantJoined
Triggered when a participant enters the room.
Example:
const { joinRoom } = useRoom({
onParticipantJoined: (participant) =>
console.log("Component: Participant joined", participant),
});
On the callback function, you will receive the following argument:
Type | Description |
---|---|
Participant | Details of the participant who joined the room. |
onParticipantLeft
Triggered when a participant leaves the room.
Example:
const { joinRoom } = useRoom({
onParticipantLeft: (participant) =>
console.log("Component: Participant left", participant),
});
On the callback function, you will receive the following argument:
Type | Description |
---|---|
Participant | Details of the participant who left the room. |
onParticipantUpdated
Triggered when a participant updates.
Example:
const { joinRoom } = useRoom({
onParticipantUpdated: (participant) =>
console.log("Component: Participant updated", participant),
});
On the callback function, you will receive the following argument:
Type | Description |
---|---|
Participant | Details of the participant that was updated. |
onRoomUpdated
Triggered when the room updates.
Example:
const { joinRoom } = useRoom({
onRoomUpdated: (data) => console.log("Component: Room updated", data),
});
On the callback function, you will receive the following argument:
Type | Description |
---|---|
RoomState | Room state of the room. |
Example:
{
status: "CONNECTED";
}
onError
Triggered when an error occurs.
Example:
const { joinRoom } = useRoom({
onError: (error) => console.error("Component: Room error", error),
});
On the callback function, you will receive the following argument:
Type | Description |
---|---|
RoomError | Error details for the room operation. |
Example:
{
"code": "auth_error",
"message": "[SuperViz] Room initialization failed: this website's domain is not whitelisted."
}
Type Definitions
RoomError
Type: object
An object describing the room error.
Name | Type | Description |
---|---|---|
code | string | The error code. |
message | string | The error message. |
Example:
{
"code": "auth_error",
"message": "[SuperViz] Room initialization failed: this website's domain is not whitelisted."
}
RoomState
Type: object
An object describing the room state.
Name | Type | Description |
---|---|---|
status | string | The room state. Options: CONNECTED , CONNECTING , DISCONNECTED , CONNECTION_ERROR , RECONNECTING , RECONNECT_ERROR |
Example:
{
"status": "CONNECTED"
}
Participant Initialization
Type: object
An object describing the participant information, like name and avatar.
Name | Type | Description |
---|---|---|
id | string | Identifier of the participant who is entering the room. Usually, this ID matches the internal ID of your users. Minimum length of 2 characters and maximum of 64. Accepted pattern: [-_&@+=,()\{\}\\[\\]\/«».:|'"#a-zA-Z0-9À-ÿ\s]* |
email | string | The email of the participant who will enter the room. |
name | string | The name of the participant who will enter the room. |
avatar | Avatar | Participant avatar information. |
Example:
{
"id": "USER_ID",
"email": "USER_EMAIL",
"name": "USER_NAME",
"avatar": {
"imageUrl": "https://<PATH>",
"model3DUrl": "https://<PATH>",
}
}
Participant
Type: object
An object describing the participant information, like name and avatar.
Name | Type | Description |
---|---|---|
id | string | Identifier of the participant who is entering the room. Usually, this ID matches the internal ID of your users. Minimum length of 2 characters and maximum of 64. Accepted pattern: [-_&@+=,()\{\}\\[\\]\/«».:|'"#a-zA-Z0-9À-ÿ\s]* |
email | string | The email of the participant who will enter the room. |
activeComponents | ComponentNames[] | List of active components that the participant has entered. |
name | string | The name of the participant who will enter the room. |
avatar | Avatar | Participant avatar information. |
Example:
{
"id": "USER_ID",
"email": "USER_EMAIL",
"name": "USER_NAME",
"activeComponents": ['videoConference'],
"type": "host",
"avatar": {
"imageUrl": "https://<PATH>",
}
}
ComponentNames
Type: enum
List of possible components that a participant can be added to. This is a read-only property, to add a participant into a component, you should use the addComponent
method of the room. The options are:
Name | Value | Component |
---|---|---|
ComponentNames.VIDEO_CONFERENCE | videoConference | Video Conference or Video Huddle |
ComponentNames.WHO_IS_ONLINE | whoIsOnline | Who-is-Online |
ComponentNames.COMMENTS | comments | Contextual Comments |
ComponentNames.PRESENCE | presence | Mouse Pointers |
ComponentNames.PRESENCE_AUTODESK | presence3dAutodesk | Presence 3D Autodesk |
ComponentNames.PRESENCE_MATTERPORT | presence3dMatterport | Presence 3D Matterport |
ComponentNames.PRESENCE_THREEJS | presence3dThreejs | Presence 3D Three.js |
Avatar
Type: object
Avatars represent participants within a room. In typical scenarios, they appear as profile images when used with Who-is-Online and Contextual Comments.
In 3D spaces (e.g., Matterport, Autodesk viewer or ThreeJs), avatars can take the form of 3D digital representations.
Name | Type | Description |
---|---|---|
imageUrl | string | Required. Path to the thumbnail image of the 3D model. Supported formats: .png and .jpg . |
model3DUrl | string | Path to a 3D model. Only applicable when using Presence3D components Supported formats: .glb and .gltf . |
Example:
avatar: {
imageUrl: "https://<PATH>",
model3DUrl: "https://<PATH>",
}