In the case of Qlik Sense Desktop, all you need to do is define a WebSocket variable, including its connection string in the following way:
var ws = new WebSocket("ws://localhost:4848/app/");
Once the connection is opened and checking for ws.open(), you can call additional methods to the engine using ws.send().
This example will retrieve the number of available documents in my Qlik Sense Desktop environment, and append them to an HTML list:
<html>
<body>
<ul id='docList'>
</ul>
</body>
</html>
<script>
var ws = new WebSocket("ws://localhost:4848/app/");
var request = {
"handle": -1,
"method": "GetDocList",
"params": {},
"outKey": -1,
"id": 2
}
ws.onopen = function(event){
ws.send(JSON.stringify(request));
// Receive the response
ws.onmessage = function (event) {
var response = JSON.parse(event.data);
if(response.method != ' OnConnected'){
var docList = response.result.qDocList;
var list = '';
docList.forEach(function(doc){
list += '<li>'+doc.qDocName+'</li>';
})
document.getElementById('docList').innerHTML = list;
}
}
}
</script>
The preceding example will produce the following output on your browser if you have Qlik Sense Desktop running in the background:
If you wish to create a WebSocket connection directly to an app, you can extend the configuration URL to include the application name, or in the case of the Qlik Sense Enterprise, the GUID. You can then use the method from the app class and any other classes as you continue to work with objects within the app.
var ws = new WebSocket("ws://localhost:4848/app/MasteringQlikSense.qvf");