Detecting User Joins / Disconnects

Understanding the VDO.Ninja IFRAME API: Detecting User Joins and Disconnects

The VDO.Ninja IFRAME API allows websites to embed and interact with VDO.Ninja streams. One of the most useful features is the ability to detect when users join or disconnect from your stream through event messaging. This guide will explain how to implement this functionality in your own projects.

How the IFRAME API Works

VDO.Ninja's IFRAME API uses the browser's postMessage API to communicate between your parent website and the embedded VDO.Ninja iframe. This allows you to:

  1. Send commands to control the VDO.Ninja instance

  2. Receive events and data from the VDO.Ninja instance

Setting Up the Basic Structure

First, you need to create an iframe that loads VDO.Ninja:

// Create the iframe element
var iframe = document.createElement("iframe");

// Set necessary permissions
iframe.allow = "camera;microphone;fullscreen;display-capture;autoplay;";

// Set the source URL (your VDO.Ninja room)
iframe.src = "https://vdo.ninja/?room=your-room-name&cleanoutput";

// Add the iframe to your page
document.getElementById("container").appendChild(iframe);

Setting Up the Event Listener

To detect joins and disconnects, you need to set up an event listener for messages from the iframe:

Detecting User Joins and Disconnects

The key events to watch for are:

Guest Connections

Disconnections

Complete Working Example

Here's a complete example that demonstrates detecting joins and disconnects:

Waiting Room Example

You can implement a waiting room like the one in the waitingroom.html file from your code samples:

Getting Additional Information About Connections

For more detailed information about connections, you can use the getStreamIDs or getDetailedState commands:

Best Practices

  1. Always check the source: Make sure messages are coming from your VDO.Ninja iframe.

  2. Handle disconnections gracefully: Sometimes connections drop unexpectedly.

  3. Consider implementing reconnection logic: If important users disconnect, you might want to notify them or attempt to reconnect.

  4. Debug with console.log: Log all events during development to understand the full message flow.

  5. Test with multiple users: The behavior can be different depending on who connects first.

By implementing these techniques, you can build sophisticated applications that respond to users joining and leaving your VDO.Ninja sessions, creating more interactive and responsive experiences.

Last updated

Was this helpful?