Listening to Event Emitters
After initializing the Videoedge SDK and successfully joining a room, the next step is to set up listeners for various event emitters. These events allow your application to respond to changes in the room, such as when new peers join, when audio or video streams start or stop, and when peers leave the room.
Why Listen to Event Emitters?
Listening to event emitters is crucial for creating a responsive and interactive user experience in your application. By handling these events, you can:
- Update the UI when new participants join or leave.
- Manage audio and video streams effectively.
- Provide feedback to users about the status of their peers.
Setting Up Event Listeners
Once you have initialized the SDK and joined a room, you can set up listeners for the following events:
- newPeer: Triggered when a new peer joins the room.
- micStart: Triggered when a peer starts sending audio.
- videoStart: Triggered when a peer starts sending video.
- micEnd: Triggered when a peer stops sending audio.
- videoEnd: Triggered when a peer stops sending video.
- peerLeft: Triggered when a peer leaves the room.
- peerMuted: Triggered when a peer is muted.
- peerUnMuted: Triggered when a peer is unmuted.
For more events check methods and events page.
Sample Code
Here’s a sample code snippet demonstrating how to listen to these event emitters:
// Assuming videoedge has been initialized and you have joined a room
// Listen for a new peer joining the room
videoedgeInstance.on("newPeer", ({ peerId, peerName, type }) => {
console.log(`New peer joined: ${peerName} (ID: ${peerId})`);
});
// Listen for when a peer starts sending audio
videoedgeInstance.on("micStart", ({ peerId, audioTrack type }) => {
console.log(`Peer ${peerId} started sending audio.`);
});
// Listen for when a peer starts sending video
videoedgeInstance.on("videoStart", ({ peerId, videoTrack }) => {
console.log(`Peer ${peerId} started sending video.`);
});
// Listen for when a peer stops sending audio
videoedgeInstance.on("micEnd", ({ peerId, type }) => {
console.log(`Peer ${peerId} stopped sending audio.`);
});
// Listen for when a peer stops sending video
videoedgeInstance.on("videoEnd", ({ peerId, type }) => {
console.log(`Peer ${peerId} stopped sending video.`);
});
// Listen for when a peer leaves the room
videoedgeInstance.on("peerLeft", ({ peerId, type }) => {
console.log(`Peer ${peerId} has left the room.`);
});
videoedgeInstance.on("peerMuted", ({ peerId, type }) => {
console.log(`Peer ${peerId} has left the room.`);
});
Summary
By setting up these event listeners, your application can effectively respond to changes in the room's state, providing a more interactive experience for users. Make sure to implement appropriate UI updates or notifications based on these events to enhance user engagement. Feel free to customize the event handling logic to fit your application's specific requirements and improve the overall user experience.