Wednesday 10 September 2014

A simple websocket client

As we discovered in a previous post, Qlik Sense Desktop communicates with the internal Qlik Sense service process primarily over the websocket protocol (WS) which as we saw can be readily inspected with Fiddler. For the curious among us, this brings within reach the creation of entirely custom Qlik Sense client applications to cater for special requirements and interesting scenarios. In this post, we're going to create the simplest example I can think of to set you on your own "custom client" journey. In this example we'll fashion a .NET Console Application in C# to request the list of documents held in Qlik Sense Desktop and display it in JSON format which is of course simply the raw response from the internal Qlik Sense service (I did say this was a simple example :) ).

This post assumes that you've the .NET Framework 4.5 and a C# IDE such as Visual Studio installed locally. If you don't have Visual Studio, you may want to consider downloading and installing the free Visual Studio Express 2013 for Windows Desktop. Once you've determined that you have everything you need, please follow the steps below.
  1. Create a .NET Console Application project in the C# IDE of your choice and call it "SenseConsoleApp".
  2. In the newly created project, create a C# class file labelled "SenseWebSocketClient.cs" and enter the following code:
  3.  using System;  
     using System.Collections.Generic;  
     using System.Linq;  
     using System.Net.WebSockets;  
     using System.Text;  
     using System.Threading;  
     using System.Threading.Tasks;  
     namespace SenseConsoleApp  
     {  
       public class SenseWebSocketClient  
       {  
         private ClientWebSocket _client;  
         public Uri _senseServerURI;  
         public SenseWebSocketClient(Uri senseServerURI)  
         {  
           _client = new ClientWebSocket();  
           _senseServerURI = senseServerURI;  
         }  
         public async Task<string> GetDocList()  
         {  
           string cmd = "{\"method\":\"GetDocList\",\"handle\":-1,\"params\":[],\"id\":7,\"jsonrpc\":\"2.0\"}";  
           await _client.ConnectAsync(_senseServerURI, CancellationToken.None);  
           await SendCommand(cmd);  
           var docList = await Receive();  
           return docList;  
         }  
         private async Task ConnectToSenseServer()  
         {  
           await _client.ConnectAsync(_senseServerURI, CancellationToken.None);  
         }  
         private async Task SendCommand(string jsonCmd)  
         {  
           ArraySegment<byte> outputBuffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(jsonCmd));  
           await _client.SendAsync(outputBuffer, WebSocketMessageType.Text, true, CancellationToken.None);  
         }  
         private async Task<string> Receive()  
         {  
           var receiveBufferSize = 1536;  
           byte[] buffer = new byte[receiveBufferSize];  
           var result = await _client.ReceiveAsync (new ArraySegment<byte>(buffer), CancellationToken.None);  
           var resultJson = (new UTF8Encoding()).GetString(buffer);  
           return resultJson;  
         }  
       }  
     }  
    
  4. In "Program.cs", delete all contents and enter the following:
     using System;  
     using System.Collections.Generic;  
     using System.Linq;  
     using System.Text;  
     using System.Threading.Tasks;  
     using System.Net.WebSockets;  
     using System.Threading;  
     namespace SenseConsoleApp  
     {  
       class Program  
       {  
         static void Main(string[] args)  
         {  
           GetDocList();  
           Console.ReadLine();  
         }  
         static async Task<string> GetDocList()  
         {  
           var client = new SenseWebSocketClient(new Uri("ws://localhost:4848"));  
           Console.WriteLine("Connecting to Qlik Sense...");  
           Console.WriteLine("Getting document list...");  
           var docs = await client.GetDocList();  
           Console.WriteLine(docs);  
           return docs;  
         }  
       }  
     }  
    
  5. Launch Qlik Sense Desktop so the local server process runs and can be queried by our console application.
  6. Now press F5 to run the application. After a short pause, you should see the list of your Qlik Sense Desktop documents in a JSON format.
    Console application listing the Qlik Sense documents

To summarise, we've sent a JSON command to the Qlik Sense Desktop's local server over the websocket protocol and retrieved the list of documents in a simple console application. The GetDocList JSON command we've sent is as follows:
 {"method":"GetDocList","handle":-1,"params":[],"id":7,"jsonrpc":"2.0"}, "requestPartCount": "1"}  
For other command examples I recommend that you use Fiddler to monitor the websocket traffic between the Qlik Sense Desktop client and the local Qlik Sense server process as described in this previous post and look at the JSON payload in the client requests. As for the technology you use to implement your custom Qlik Sense client, all you need is a websocket library and a JSON library. My background is in C# which is why I opted for .NET but you should also be able to do this in Java, Python and of course HTML5 to mention a few.

I hope you've found this stimulating. Thank you for reading.

No comments:

Post a Comment