Communicate with your App from iframe - HTML
Â
Source: GitHub - e3ds/E3DS-Iframe-Demo at iframe-demo-html
Iframe Demo: E3DS Iframe Demo
ReactJS Iframe Demo: Eagle React Demo
UE5 Iframe Demo: E3DS Iframe Demo
Unreal project source code used in the demo: Bitbucket
Overview
Communication refers to the transmission of data between two individuals or entities, where one sends data and the other receives it, and vice versa.
1. Sending data from the parent Webpage to the Unreal App
To communicate with the unreal app, you should send data from the webpage to the unreal app and the app will send you a response.
The data must be sent as a string to the unreal app.
To send Integer data, we have to convert the integer into a string using the toString()
method.
To send a JSON object, we should stringify it using the JSON.stringify()
method.
For an array, we need to convert it to a JSON object and then stringify the object.
Below is an example of how to send JSON data from the webpage to the unreal app.
From the Javascript client page:
let descriptor = {
Teleport: "6"
};
let obj = {
cmd: "sendToUe4",
value: descriptor
}
document.getElementById("iframe_1").contentWindow.postMessage(JSON.stringify(obj), "*");
//here "iframe_1" is the id of the HTML iframe element
2. Manipulating the style attribute of an HTML element
This can also be achieved by sending data from the webpage but we need to make a few changes in the JSON object for that.
Here’s an example that hides the settings button within the streaming UI:
id is the ID of the HTML element whose style you are trying to change.
const toggleSettings = (id)=>{
  let descriptor = {
    id,
    property: 'display',
    value: 'none'
  };
  let obj =
    {
      cmd: "style",
      value: descriptor,
    }
  document.getElementById("iframe_1").contentWindow.postMessage(JSON.stringify(obj), "*");
}
3. Receiving data from the Unreal App to the parent Webpage
After sending the data we will receive a response from the unreal app.
This response can be caught by using message event handler defined in the page
.
Below is a detailed example of that:
function HandleResponseFromUE4(jsonObj) {
// Process the JSON object sent by the Unreal app to the webpage
}
const eventHandler = (event) => {
// This function will receive all kinds of events the webpage receives
// Check if event.data.type exists to identify if it was sent by the Unreal app
if (!event.data.type) {
// If event.data includes a field named cmd, then this data is coming from the Unreal app.
// If your Unreal app uses a different field name, replace this field name with the one you have used in your Unreal app
if (typeof event.data === 'string' && event.data.includes('cmd')) {
// We are receiving data as a string. Now we try to parse it to check
// if this string represents a JSON or not.
// If you are sending a normal string from the Unreal app, then this is not needed
const parsedData = JSON.parse(event.data);
if (parsedData.cmd) { // It is JSON object data sent from UE4
HandleResponseFromUE4(parsedData); // Process the data sent by UE4
}
}
}
else{
//data sent from server
messageHandler(event);
}
}
// We are binding eventHandler to receive all kinds of messages the webpage receives
window.addEventListener("message", eventHandler);
4. Receiving Non-Unreal Data from an Iframe on a Webpage
If a message doesn’t contain event.data.type
, it is sent to messageHandler
function.
See this document to learn different types of event sent to messageHandler
.
5. Setting a Custom Resolution via an Iframe on a Webpage
Refer to this document to learn how to set a custom resolution on your webpage.
6. Sending a Pixel Streaming Response from the Unreal App to the Webpage
Refer to this document.
Useful Events
See this document for Useful Events in Iframe Solution.
Â
Need help? Contact Support
Submit a new request at E3DS support portal.
Requests sent on weekends will not be addressed until the following business day.