2 Replies Latest reply: Oct 10, 2017 2:10 AM by Joel Wikström RSS

    Fail to reload Qlik Sense dashboard app through engine api and websocket

    Joel Wikström

      Hi!

       

      I have created a Qlik Sense Desktop applikation and I want update it every five minutes and show it as a dashboard in a web page as an embedded iFrame configured in the Single configurator.

       

      I have made a script that connects to the engine api through a web socket and sends openDoc, doReload and saveObject every five minutes. The script runs without errors. The script log is generated, the qvf file is updated and csv-file is exported correctly at the end of the script but the data in the application is not updated.

       

      In the script I create a variable as let vDate = now(); which I show in a text box and that value is updated but nothing else in the GUI. My only explanation to this is that the data in the application is never updated when the script runs.

       

      How could I script the reload of an application through the eninge API?

      Do I need to save the data after reloading the application?

       

      Below is my html page embedding the dashboard with a script reloading the application every five minutes:

      <html>

        <head>

          <title>Dashboard</title>

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

          <script type="text/javascript">

       

      //Engine API request bodies

      var openDoc = {

        "method": "OpenDoc",

        "handle": -1,

        "params": [

          "C:\\Users\\xxx\\Documents\\Qlik\\Sense\\Apps\\xxxxx.qvf"

        ],

        "outKey": -1,

        "id": 2

      }

       

      var doReload = {

        "handle": 1,

        "method": "DoReload",

        "params": {

          "qMode": 0,

          "qPartial": false,

          "qDebug": false

        }

      }

       

      var saveObjects = {

        "handle": 1,

        "method": "SaveObjects",

        "params": {},

        "outKey": -1,

        "id": 4

      }

       

       

      //Helper function for logging

      function logga(text)

      {

      var time = new Date();

      var timeStr = time.toLocaleTimeString();

      var str = timeStr + " " + text;

      console.log(str)

      }

       

       

      //Function that prepares a function that communicates with Qlik throuh the engine API

      //Sends msg to the websocket ws and applies next once a responce is received.

      //No error detection implemented

      function wsCommand(ws, msg, name, next) {

        return function (evt) {

          ws.send(JSON.stringify(msg));

          logga(name + " message is sent..."  + JSON.stringify(msg, null, 2));

       

       

          ws.onmessage = function (evt) {

            var received_msg = evt.data;

            logga(name + "Message is received: " + JSON.stringify(received_msg, null, 2));

            next();

          }

        }

      }

       

       

      //Functionality to reload the whole page and not just the iFrame in order to clear console log

      var reloadCounter = 0;

       

       

      //Function to refresh the dashboard as the last step in the command chain

      function wrapUp(ws) {

        return function() {

          ws.close();

          logga("Done!");

          reloadCounter += 1;

          if(reloadCounter < 10) {

            logga("Refresh count " + reloadCounter);

            refreshDashboard();

          } else {

            window.location.reload() //Reload the whole page and not just the iFrame in order to clear console log

          }

        }

      }

       

       

      function reloadAndRefresh() {

        if ("WebSocket" in window) {

           logga("WebSocket is supported by your Browser!");

       

       

           var ws = new WebSocket("ws://localhost:4848/app");

       

       

           ws.onmessage = function(evt) {

             var received_msg = evt.data;

             logga(name + "Message is received: " + JSON.stringify(received_msg, null, 2));

       

       

             //1. open doc -> 2. doReload -> 3. saveObjects

             var saveObjectsCmd = wsCommand(ws, saveObjects, 'saveObjects', wrapUp(ws));

             var doReloadCmd = wsCommand(ws, doReload, 'doReload', saveObjectsCmd);

             var openDocCmd = wsCommand(ws, openDoc, 'openDoc', doReloadCmd);

       

       

             openDocCmd()

           }

       

       

           ws.onclose = function() {

              logga("Connection is closed...");

           };

       

       

           window.onbeforeunload = function(event) {

             logga("Closing...");

              ws.close();

           };

        }

        else {

           // The browser doesn't support WebSocket

           logga("WebSocket NOT supported by your Browser!");

        }

      }

       

       

          //Update the frame with the dashboard. Add a new IFrame instead of refresh due to cross browsing issues

          function refreshDashboard() {

            var d = $("#dashboard");

            d.empty();

            d.append(`

            <iframe

              src='http://localhost:4848/single/?appid=C%3A%5CUsers%5Cxxx%5CDocuments%5CQlik%5CSense%5CApps%5Cxxxxxx.qvf&sheet=6d58f047-cd7b-4a1f-bdda-68a9fbefd694&opt=nointeraction&select=clearall'

              name="QlikDashboard"

              style='border:none;'

              height='800'

              width='1200'

              >

            </iframe>

            `)

          }

       

       

          </script>

       

       

        </head>

        <body>

       

       

          <div id="dashboard"></div>

       

       

          <script type="text/javascript">

            refreshDashboard()

            reloadAndRefresh();

            setInterval(reloadAndRefresh, 5 * 60 * 1000 );

          </script>

        </body>

      </html>