HTML Bridge Guide

This article provides best practices and guides on how to import interactive custom HTML iFrames into the JourneyApps runtime using the journey-iframe-client. This library is used inside an HTML file and allows your JourneyApps application and the HTML file to communicate with one another. This communication is achieved through combining the following methods in the HTML file and the application View JS|TS.

post(cmd: string, param1?: any, ..., paramN?: any) : Promise<any>
Send a message from the HTML file to the app or from the app to the HTML file. The application will show a blocking spinner when posts are executed. The message is received on the other side by listening for the specified cmd: string using the on() method.

postNonBlocking(cmd: string, param1?: any, ..., paramN?: any) : Promise<any>
Send a message from the HTML file to the app or from the app to the HTML file, without showing a blocking spinner. The message is received on the other side by listening for the specified cmd: string using the on() method.

on(cmd: string, (param1?: any, ..., paramN?: any) => any)
Listen for messages sent from the html file to the app or from the app to the html file using either the post() or postNonBlocking() methods. The method listens for messages on the specified cmd: string.

You can find more information on communicating with the HTML Component here in our documentation.

Examples

In the two examples below, we’ll display a line chart using Chart.js containing a count of the US population by year, which is stored in our JourneyApps DB. Each of these DB objects will have a count, nation, and year fields. We’ll then render the count by year for the United States.

Data Model & Sample Data

Follow the steps below to set up the data model and fetch the population data which will seed the JourneyApps DB with sample data.

  1. Update your Data Model and include the following model:
 <model name="population" label="Population">
       <field name="nation" label="Nation" type="text" />
       <field name="year" label="Year" type="text" />
       <field name="count" label="Count" type="number" />
      
       <display>{nation} {year}</display>
</model>
  1. Let use the Data USA API to get the population count by year and nation, then save this to DB objects in JourneyApps. Each population object will contain a nation, year, and population count. Start by creating a function called getData in our main.js:
function getData() {
   var url = "https://datausa.io/api/data?drilldowns=Nation&measures=Population";
   var options = {
       method: "GET",
       headers: {
           "Content-Type": "application/json"
       }
   };

   return fetch(url, options)
       .then(function (response) {
           if (response.status < 200 || response.status >= 300) {
               // Request failed
               throw new Error("Failed to make network request: [" + response.status + "]");
           } else {
               return response.json();
           }
       }).then(function (json) {
           var populationArrary = json.data;
           console.log(JSON.stringify(populationArrary, null, 2));
           for(var i = 0; i < populationArrary.length; i++) {
               notification.info("Syncing " + (i + 1) + "/" + populationArrary.length);
               var population = DB.population.first("nation = ? and year = ?", populationArrary[i].Nation, populationArrary[i].Year);
               if(!population) {
                   population = DB.population.create();
                   population.year = populationArrary[i].Year;
                   population.nation = populationArrary[i].Nation;
                   population.count = populationArrary[i].Population;
                   population.save();
               }
           }
           return notification.info("Request completed");
       }).caught(function (error) {
           dialog("Error fetching population data", error.message);
       });
}
  1. Call the getData function in the init() function of the main.js:
function init () {
     getData();
}

Basic

In the Basic example, we’ll create a single HTML file and upload this file into Oxide for further editing.

  1. Create a new HTML file e.g. chart.html using the following template:
<!DOCTYPE html>
<html lang="en" style="height: 100%;">

<head>
   <meta charset="UTF-8" />
   <title>File</title>
   <!--Reference to the journey-iframe-client, must have in the file-->
   <script src="https://cdn.jsdelivr.net/npm/journey-iframe-client@0.2.0/dist/bundle.min.js"></script>
   <!--Here is the reference to chart.js -->
   <script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4/dist/Chart.bundle.min.js"></script>
   <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes, minimum-scale=0.25, maximum-scale=5.0" />
</head>

  <body>
  </body>

</html>

Notice that we added two script elements, which include the journey-iframe-client and ChartJS. This is the easiest way to reference the iFrame client in your HTML file.

  1. Upload this file to Oxide by navigating to the Assets workspace and dragging/dropping the chart.html file created in the previous step into the Assets\html directory as shown on the left-hand side of the Assets panel.
  2. Open the uploaded file in Oxide and update the body of the file to include the following:
<body style="height: 100%;">
  <style type="text/css">
      .container {
          display: flex;
          flex-direction: row;
          flex-wrap: wrap;
          justify-content: space-evenly;
      }

      .chart-container {
          position: relative;
          margin: auto;
          height: auto;
          width: max(400px, 30vw);
      }
  </style>
  <h3 id="status" style="font-family: monospace;"> Loading Chart </h3>
  <div class="container">
      <div class="chart-container">
          <canvas id="chart_canvas" width="600" height="600"></canvas>
      </div>
  </div>
  <script>
       var client = new JourneyIFrameClient();
       window.addEventListener('DOMContentLoaded', function () {
           client.post("mounted", true)
               .then(function (result) {
                   if(result == "success") {
                       var status = document.getElementById('status');
                       status.remove();
                   }
               });

           client.on("render", function (chartConfig) {
               var ctx = document.getElementById("chart_canvas");
               var chart = new Chart(ctx, chartConfig)
           });
       });
  </script>
</body>

The update to the body includes the following changes:

  • We added a few style attributes for our chart containers
  • Added an h3 element that displays Loading Chart, but this gets removed when the JourneyApps container returns a successful response
  • We defined two div tags. One will act as a container, and another will hold our canvas element. This is where the ChartJS chart will be rendered when the JourneyApps runtime posts the population data to the iFrame.
  • We create a new script element that initializes the JourneyIFrameClient
  • The script also includes an event listener that fires when the HTML has completely loaded (DOMContentLoaded). This will post a message to the JourneyApps runtime letting it know it’s ready and can start accepting data from the JourneyApps runtime
    • This is done by the following: client.post("mounted", true), which sends a message from the HTML file to the app JS runtime.
    • The app JS listens for messages on “mounted” with a on(“mounted”) method definition and returns true once it receives a message on this ‘channel’
  • Finally, the script includes a JourneyIFrameClient event listener in the HTML file. This tells the JourneyIFrameClient to wait for the JourneyApps runtime to post data to iFrame. In this case, it will post to the ChartJS configuration object. This is done with the client.on("render") definition.
  1. Now that we have an event listener waiting to receive the ChartJS configuration, we will update the main view’s javascript in Oxide to post data to the JourneyiFrameClient. Let’s start by adding the chart config above the init() function as a global view variable:
var chartConfig = {
   type: 'line',
   data: {
       labels: [],
       datasets: [{
           label: 'US Population',
           data: [],
           backgroundColor: [
               'rgba(255, 99, 132, 0.2)',
               'rgba(54, 162, 235, 0.2)',
               'rgba(255, 206, 86, 0.2)',
               'rgba(75, 192, 192, 0.2)',
               'rgba(153, 102, 255, 0.2)',
               'rgba(255, 159, 64, 0.2)'
           ],
           borderColor: [
               'rgba(255, 99, 132, 1)',
               'rgba(54, 162, 235, 1)',
               'rgba(255, 206, 86, 1)',
               'rgba(75, 192, 192, 1)',
               'rgba(153, 102, 255, 1)',
               'rgba(255, 159, 64, 1)'
           ],
           borderWidth: 1
       }]
   },
   options: {
       scales: {
           yAxes: [{
               ticks: {
                   beginAtZero: true
               }
           }]
       }
   }
};
  1. Next, we will update the init() function to include the following:
function init() {
   component.html({ id: "frame" }).on("mounted", function (bool) {
       updateConfig();
       component.html({ id: "frame" }).post("render", chartConfig);
       return "success";
   });
}

The update to the init() function includes the following changes:

  • First, we add an on event that listens for the mounted event from the iFrame. Without the event listener, it is not possible to post to the JourneyiFrameClient before it has completed the DOMContentLoaded event. In this case, the JourneyApps runtime will not recognize the iFrame and will fail to post.
  • Once the mounted event is invoked we execute the updateConfig function, which queries the database and updates the config. We’ll add this in the next step.
  • Once that function has completed we post the chartConifg object to the render event that is in the iFrame.
  • We then return success to let the iFrame know that the job is completed, which then removes the h3 element.
  1. Add the updateConfig function in your main view below the init() function. This function is responsible for retrieving the population data from the DB and updating the chartConfig with the correct labels and data to display on the chart:
function updateConfig () {
   var populationData = DB.population.all().orderBy("year").toArray();
   var labels = populationData.map(function (population) {
       return population.year;
   });
   var stock_levels = populationData.map(function (population) {
       return population.count;
   });
   chartConfig.data.labels = labels;
   chartConfig.data.datasets[0].data = stock_levels;
}  
  1. Finally, we will add the HTML component to our main view XML to display the iFrame:
<view title="US Population">
   <html id="frame" src="html/chart.html" show-fullscreen-button="true"/>
</view>

Notice that we added an id attribute with a value of frame. This can be used when you have multiple HTML iframes and need to target specific post or on events. We also reference this id when targeting the component in the runtime using component.html({ id: "frame" }).
8. Deploy and test. The result should look like this:


9. To customize the chart, update the chartConfig based on options available on ChartJS.

Advanced

In this guide, similar to the Basic guide above, we’ll be creating the HTML file locally and will then upload it to a JourneyApps application. However, all of the development of the iFrame will be done locally in this advanced guide. We achieve this by defining an HTML file and injecting compiled JavaScript into the file. This is especially helpful when developing more advanced iFrame clients.

Prerequisites

  • Node.js: You can install the latest version directly from the Node.js site, or install the latest version using nvm.
  • Yarn: You can install the latest version of yarn here.
  • Complete the Data Model & Sample Data section of this guide to configure and populate the data used in this example

Setup

  1. Create a new directory on your machine, initialize a Node.js application and create the basic file/folder structure for the project.
> mkdir chart
> cd chart
> yarn init
  1. Install the journey-iframe-client using yarn

yarn add journey-iframe-client

  1. Create the following file/folder structure

In this example, we are not going to write the JS in our HTML as we did in the basic example. Instead, we are going to write our JS in the index.js file and use webpack to compile our JS and HTML files into a single file, which we will upload to Oxide.

  1. Update your package.json file and add the following as a script node:
"scripts": {
   "build": "webpack"
}
  1. Update your package.json with the following to your devDependencies:
"devDependencies": {
    "@types/node": "^10.11.6",
    "css-loader": "^1.0.0",
    "html-webpack-inline-source-plugin": "^0.0.10",
    "html-webpack-plugin": "^3.2.0",
    "node-sass": "^4.9.3",
    "sass-loader": "^7.1.0",
    "style-loader": "^0.23.1",
    "superagent": "^4.0.0-beta.5",
    "ts-loader": "^5.2.1",
    "typescript": "^3.1.1",
    "webpack": "^4.20.2",
    "webpack-cli": "^3.1.2",
    "yaml": "^1.0.0"
}
  1. Run yarn install to update your node modules and install all the devDependencies
  2. Add the following to your webpack.config.js file:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
const yaml = require('yaml');
const fs = require('fs');

const config = yaml.parse(fs.readFileSync('config.yml', 'utf8'));
const {outputFileName, htmlTitle} = config;

module.exports = {
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js',
    },
    plugins: [
        new HtmlWebpackPlugin({
            title: htmlTitle,
            template: './src/template.html',
            filename: outputFileName,
            inlineSource: '.(js css ts)$'
        }),
        new HtmlWebpackInlineSourcePlugin()
    ],
    resolve: {
        extensions: ['.ts', '.tsx', '.js']
    },
    module: {
        rules: [
            { test: /\.tsx?$/, loader: 'ts-loader' },
            {
                test: /\.scss$/,
                use: [
                    "style-loader", // creates style nodes from JS strings
                    "css-loader", // translates CSS into CommonJS
                    "sass-loader" // compiles Sass to CSS, using Node Sass by default
                ]
            }
        ]
    }
};

You’ll also notice that we added rules to our webpack config. These will allow us to style our iFrame using SASS and we’ve added rules for TypeScript.

  1. Add the following to your config.yml file:
outputFileName: output.html
htmlTitle: Template name

Replace the values of the outputFileName and htmlTitle as you see fit. These are used by webpack when the file is compiled and sets the output HTML filename as well as the name of the title of the HTML file.

Development

  1. Open the src/template.html file and add the following:
<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4/dist/Chart.bundle.min.js"></script>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes, minimum-scale=0.25, maximum-scale=5.0" />

        <title>
            <%= htmlWebpackPlugin.options.title %>
        </title>
    </head>

    <body>

    </body>
</html>
  1. If you want to store this source code in a remote repository, please make sure you update your .gitignore file accordingly. Here is an example:
dist
node_modules
.DS_Store

You won’t need to push the dist or node_modules files to your remote repository.

  1. Update the body of the src/template.html with the following:
<body>
    <h3 id="status" style="font-family: monospace;"> Loading Chart </h3>
    <div class="container">
        <div class="chart-container">
            <canvas id="chart_canvas" width="600" height="600"></canvas>
        </div>
    </div>
</body>
  1. Update the src/index.js with the following:
const JourneyIFrameClient = require('journey-iframe-client');
const client = new JourneyIFrameClient();

window.addEventListener('DOMContentLoaded', function () {
    this.console.log('Window has loaded, posting mounted event');
    client.post("mounted", true)
        .then(function (result) {
            if(result == "success") {
                var status = document.getElementById('status');
                status.remove();
            }
        });

    client.on("render", function (chartConfig) {
        var ctx = document.getElementById("chart_canvas");
        var chart = new Chart(ctx, chartConfig);
    });
});

Similar to our basic example, we’ll add an event listener that fires when DOMContentLoaded occurs. This will post to the JourneyApps runtime informing it that it has loaded and is ready to accept messages.

  1. Add a few styles to your src/sass/styles.scss:
body {
    height: 100%;
}

.container {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-evenly;
}

.chart-container {
    position: relative;
    margin: auto;
    height: auto;
    width: max(400px, 30vw);
}
  1. Run the yarn build command in the terminal. This will run the webpack command and compile all the JS files, SASS files and inject them into the template.html file. The result is found in the dist/output.html (or whatever you specified as the value for the outputFileName key in the config.yml file).

  2. Upload the compiled HTML file to Oxide under the Assets workspace within the HTML directory.

  3. Tip: Avoid opening the compiled HTML file on Oxide. This file is the compiled version of your iFrame client and should not be edited in its compiled form.

  4. Follow steps 3 - 7 in the Basic example of this post. These steps will guide you through the process of adding the HTML component to the view XML, retrieving the sample population data from the database, and finally posting the data to the JourneyIFrameClient, which will render the chart and display the data. Please note that if you updated the outputFileName key in the config.yml file, make sure you update the src attribute of the JourneyApps HTML component to reference the correct HTML file uploaded to Oxide.

  5. Test it in your app to ensure the data is showing correctly.

The complete source code of this HTML iFrame can be found here.