Qlik Community

Qlik Sense Integration, Extensions, & APIs

Discussion board where members can learn more about Integration, Extensions and API’s for Qlik Sense.

Announcements
BARC’s The BI Survey 19 makes it official. BI users love Qlik. GET REPORT
Highlighted
nil-1995
New Contributor II

Can I get values from a table via a WebSocket?

I created a table in Qlik Sense with dimensions and measures. Also one measure has an expression. Why I can't see the expression on console (another fillds I can see)? How to get the content of the table using Websocket? I want to show the table using only table's ID.

//connection

var WebSocket = require('ws');

var fs = require('fs');

var certificates = {

     cert: fs.readFileSync('./certificats/client.pem'),

    key: fs.readFileSync('./certificats/client_key.pem'),

    root: fs.readFileSync('./certificats/root.pem')

};

var ws = new WebSocket("wss://server.domen:9999/app/", {

     ca: certificates.root,

     cert: certificates.cert,

     key: certificates.key,

     headers: {

          'X-Qlik-User':  'UserDirectory=internal; UserId=sa_engine'

     }

});

//queries

ws.onopen = function (event) {

         var OpenDoc = {

          "method": "OpenDoc",

          "handle": -1,

          "params": [

          "AppID"

          ],

          "outKey": -1,

          "id": 1

}

var GetObject = {

     "handle": 1,

     "method": "GetObject",

     "params": {

     "qId": "ObjectID"

     },

     "outKey": -1,

     "id": 3

}

var GetLayout = {

     "handle": 2,

     "method": "GetLayout",

     "params": {},

     "outKey": -1,

     "id": 4

}

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

ws.onmessage = function (event) {

   var response = JSON.parse(event.data);

   console.log(response); 

};

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

ws.onmessage = function (event) {

   var response = JSON.parse(event.data);

   console.log(response); 

};

setTimeout(function() { ws.send(JSON.stringify(GetFullPropertyTree));

     ws.onmessage = function (event) {

     var response = JSON.parse(event.data);

     var dimensionInfo = response.result.qLayout.qHyperCube.qDimensionInfo;

     var measureInfo = response.result.qLayout.qHyperCube.qMeasureInfo;

     dimensionInfo.forEach(function(item, dimensionInfo) {

          console.log(item.qFallbackTitle);

     })

     measureInfo.forEach(function(item, measureInfo) {

          console.log(item.qFallbackTitle);

})

    

} }, 2000);

};

6 Replies
Partner
Partner

Re: Can I get values from a table via a WebSocket?

Hello Илья,

I think you made a pasting error (you create a variable called GetLayout and then later stringify a variable GetFullPropertyTree) but that shouldnt really matter.

I always use the Engine-API-Explorer for these kind of things because it is very helpful (/dev-hub/engine-api-explorer):dev-hub.png

When looking at the result of GetFullPropertyTree we can see that there is no property named "qMeasureInfo" inside the qHyperCubeDef, only a property called "qMeasures".Inside the individual measure's objects is another property called qDef which in turn contains the expression used for this measure.

To find the correct paths / properties just play around with the engine-api-explorer. You can also check out Qliks enigma.js which should simplify the communication with qlik sense


Cheers,


Mathias

nil-1995
New Contributor II

Re: Can I get values from a table via a WebSocket?

Thanks a lot!

Can I get field's values in this way? I can calculate measures but EvaluateEx function returns only one result. I want to calculate measures for all rows. Is it possible to get rows?

Partner
Partner

Re: Can I get values from a table via a WebSocket?

You can get values by using a hypercube (which you can construct using a HyperCubeDefinition).

For a GetObject-handle you can probably use something like this:

{

"method": "GetHyperCubeData",

"params": {

"qPath": "/qHyperCubeDef",

"qPages": [

{

"qLeft": 0,

"qTop": 0,

"qWidth": 99,

"qHeight": 99

}

]

}

Mathias

Partner
Partner

Re: Can I get values from a table via a WebSocket?

Were you able to solve this issue ?
I am also following same approach ..connecting through websocket , used methods to OpenDoc , Doreload and then want to read field values to render chart.
If you have worked out a solution can you share next steps to followed to get values of the fields.

Thanks
NK
Employee
Employee

Re: Can I get values from a table via a WebSocket?

Hypercubes and list objects are the primary entities in the engine API to use for retrieving data. A good starting point to learn about this can be this section:

https://help.qlik.com/en-US/sense-developer/April2019/Subsystems/Platform/Content/Sense_PlatformOver...

In particular, I would recommend the sub pages named "GenericObject", "Hypercube", "List object" and "Paging". There are also quite a few threads here on Community on the topic, so if you search for "Hypercube" you will find a lot of discussions that might be close to what you need.

And if you're comfortable with C#, then you can find an example project that illustrates how to use hypercubes for different purposes (data retrieval, selection, sorting, etc) here: https://github.com/kolsrud/qlik-dot-net-sdk-hypercube-usage

Partner
Partner

Re: Can I get values from a table via a WebSocket?

different approach here >>

Trying to read a file through a QS-extension object ,
Extension JS script can read file in case 1 but fails when we try case 2 as provided below :

Case 1:
var filenm = 'http://localhost:4848/Extensions/myChart/abc.log; // CASE 1 : SUCCESS when in QS Desktop it works
d3.tsv( filenm, function(data) {
return {
Timestamp: data.Timestamp,
ActiveUsers: data.ActiveUsers ,
Users: data.Users,
};

}, function(error, rows) {
if (error) { //if error is not null,
console.log('error: '+ error);
} else if (index < rows.length) {
yVal1=rows[index].ActiveUsers;
yVal2=rows[index].Users;
++index;
}
else {
//duration(10000);
}
});
...other steps

Case 2:

 var filenm = 'C:/Log/abc.log;  // CASE 2: Failed while pointing to other directory in QS dekstop

d3.tsv( filenm, function(data) {
return {
Timestamp: data.Timestamp,
ActiveUsers: data.ActiveUsers ,
Users: data.Users,
};

}, function(error, rows) {
if (error) { //if error is not null,
console.log('error: '+ error);
} else if (index < rows.length) {
yVal1=rows[index].ActiveUsers;
yVal2=rows[index].Users;
++index;
}
else {
//duration(10000);
}
});
...other steps

Do you know if /how approach 2 can work ?