Create custom drawing app

Transmitting Drawing Data Between Clients with VDO.Ninja IFRAMES

VDO.Ninja IFRAME API: Transmitting Drawing Data Between Clients

This guide explains how to use the VDO.Ninja IFRAME API to send drawing data (or any custom data) between clients using peer-to-peer (P2P) data channels.

Understanding the Data Channel

VDO.Ninja allows you to send arbitrary data between connected clients using its P2P data channels. This feature enables applications like:

  • Custom drawing/annotation tools

  • Chat systems

  • Control signals

  • Sensor data exchange

  • Any other custom data payloads

The creators of VDO.Ninja use VDO.Ninja's data-channel functionality in many of their other applications and services, including Social Stream Ninja that processes hundreds of messages per minute per peer connection.

Basic Setup

First, set up your VDO.Ninja iframe as described in the basic documentation:

// 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 Event Listeners

To receive data from other clients, set up an event listener for messages from the iframe:

Sending Data to Peers

Sending to All Connected Peers

Use this approach to broadcast data to all connected peers:

Sending to a Specific Peer by UUID

Use this approach to send data to a specific peer identified by UUID:

Sending to Peers with Specific Labels

Use this approach to send data to all peers with a specific label:

Sending to a Peer by StreamID

Use this approach when you know the streamID but not the UUID:

Drawing-Specific Implementation

For transmitting drawing data specifically, you'll need to:

  1. Create a drawing canvas on your page

  2. Capture drawing events

  3. Format the data appropriately

  4. Send the data to peers

  5. Process and render received drawing data

Here's a simplified example:

Advanced Drawing Commands

You can implement special drawing commands like clear, undo, etc.:

Using VDO.Ninja's Built-in Drawing System

VDO.Ninja has a built-in drawing system you can leverage if you prefer not to implement your own:

Complete Example: Drawing Application

Here's a more complete example of a drawing application using the data channel:

Best Practices

  1. Data Structure: Use a clear, consistent data structure for your payloads

  2. Normalization: Normalize canvas coordinates (0-1 range) to ensure consistent display across different screen sizes

  3. Throttling: Consider throttling frequent events like mouse movements to reduce data transmission

  4. Error Handling: Always include try/catch blocks when sending or processing data

  5. State Synchronization: When new peers join, send them the current state

  6. UUID vs StreamID: Use UUID for reliable targeting; StreamIDs change when connections restart

  7. Connection Status: Monitor connection and disconnection events to maintain a list of active peers

Common Types of Data to Send

  • Drawing Paths: Arrays of points representing drawing strokes

  • Commands: Clear, undo, change color, change brush size

  • Annotations: Text or shapes to overlay on videos

  • Control Signals: Camera directions, audio levels, recording commands

  • Chat Messages: Text messages between users

  • Sensor Data: Device orientation, location, acceleration

Troubleshooting

  • Data Not Arriving: Check that you're using the correct UUID or streamID

  • Timing Issues: Ensure your iframe is fully loaded before sending messages

  • Cross-Origin Issues: Make sure your security settings allow communication

  • Format Errors: Verify your data structure matches what receivers expect

  • Performance Problems: Large data payloads can cause lag; consider optimizing

By following this guide, you should be able to implement custom drawing tools or any other data-sharing features using VDO.Ninja's P2P data channels.

Last updated

Was this helpful?