useConnection Hook - React
useConnection hook abstracts connection class and takes all the properties and events as parameters and returns all the properties and methods to work connection instance.
useConnection example
import {
useConnection,
} from "@videosdk.live/react-sdk";
function onParticipantJoined(participant) {
console.log("Participant Joined", participant);
}
function onParticipantLeft() {
console.log("Participant left");
}
function onChatMessage(message) {
console.log("Chat Message: ", message)
}
const {
connection
} = useConnection({
connectionId,
onMeeting: {
{
onParticipantJoined,
onParticipantLeft,
onChatMessage
}
}
});
Parameters
connectionId
-
type :
String -
REQUIRED -
connectionIdrepresents themeetingIdyou had passed inconnectTo()
onMeeting
-
type :
Object -
OPTIONAL -
onMeetingis a object ofonParticipantJoined,onParticipantLeftandonChatMessagecallbacks for the meeting you are connected to. -
onParticipantJoined()
- This event callback is trigger when a new participant joins the connected meeting.
-
onParticipantLeft()
- This event callback is trigger when a participant leaves the connected meeting.
-
onChatMessage()
- This event callback is trigger when a new message is received in the connected meeting.
Returns
useConnectionreturns an object ofConnectionwhich has following properties and methods.
id
- type :
String - It represents the meetingId of the meeting you have established a connection with.
payload
- type :
String - Any arbitrary payload you define during the connection.
participants
- type :
[Participant] - Represents Participants of connected meeting.
meeting
-
meetingwill be of typeobjectwhich will haveid,sendChatMessage(),end()andparticipants. -
id
idwill havemeetingIdfor the meeting you are connected to.
-
sendChatMessage()
-
If you want to communicate participants of connected meetings, it can be achieved by
sendChatMessage. -
Paramaters - message :
String -
When any participant of Meeting A sends a chat message to Meeting B, then
onChatMessageevent will be emitted to all participants of Meeting B.
-
Example
connection.meeting.sendChatMessage("Hi there, from MARS!");
-
end()
end()is used to end the connected meeting.- After executing this method, all participants of that meeting will leave automatically.
Example
connection.meeting.end();
Method
close()
-
close()is used to close the connection with meeting. -
onConnectionCloseevent of meeting class is triggered to all participant wheneverconnection.close()being called.
Example
connection.close();
Got a Question? Ask us on discord

