Qlik Community

Qlik Sense Integration, Extensions, & APIs

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

Not applicable

Sharing session between Engine API and client

I am working on a .Net application that connects to Sense Engine API through Proxy.

I want to programmatically apply a filter to a specified App Field, then I want to see the same selection in the same App opened in a web browser (with the same userid used to connecto to Engine API).

In short terms i want to replicate the same behaviour of two different browser while presenting the same App, if I apply a filter in one browser I can see the selection reflected to second browser.

I dont know why this doesen't work for the session opened with Engine API (it seems a completely separated session).

I am sure that field filtering works because if I programmatically create a snapshot after a selection I can see the snapshotted object with the right filter applyed (but for the problem with session if I want to see the snapshot taht I created with ENGINE API I have first to close all opened session for the user then, when i open again the App in a browser, I can see all the snapshots).

My code for connection (at the moment I using connection via proxy but I have tryed all possible way to connect with the same result):

            var aI = location.AppWithId(MyAppId, noVersionCheck: true);

            // Default session should beshared with clients                

            var defaultSession = Session.WithApp(aI, SessionType.Default);

            using (var app = location.App(aI, session: defaultSession, noVersionCheck: true))

            {

                var field = app.GetField(MyFieldName);           

                if (field == null) return false;

                var result = field.Select(value, true);

                if (result)

                {

                    var extendedSel = app.GetExtendedCurrentSelection();

                    var selectedIndex = 0;

                    foreach (var nxDataPage in extendedSel.GetField(MyFieldName).DataPages)

                    {

                        foreach (var rows in nxDataPage.Matrix)

                        {

                            var cell = rows.FirstOrDefault();

                            if (cell != null)

                            {

                                if (cell.State == Qlik.Engine.StateEnumType.SELECTED)

                                    selectedIndex = cell.ElemNumber;

                            }

                        }

                    }

                }

                return result;

            }

4 Replies
akl
Honored Contributor

Re: Sharing session between Engine API and client

I don't know the intrinsic details of the .Net SDK so I can't help you there but perhaps I can shed some light on how session sharing is done.

The Engine will share sessions between connections if they supply the same route and the same http headers.

Route in this case would be the websocket url such as wss://server/app/guid

When connecting over the Proxy it will inject certain http headers documented here

http://help.qlik.com/en-US/sense-developer/3.2/Subsystems/ProxyServiceAPI/Content/ProxyServiceAPI/Pr...

So for your app to be able to share sessions with the connected browser then you would have to supply the same route and emulate the same http headers

Not applicable

Re: Sharing session between Engine API and client

Hi Alexander, thanks for answer.

I am investigating the the differences between headers passed from web browser to the Sense Server the headers that .NET Sdk creates in its request. They are different but I am not so expert in web requests.

Can you help me to understand what headers i have to emulate ( and if the route, as you mentioned in your reply, is the part of the string at the right of 'GET' command).

WEB BROWSER request:

GET /sense/app/3772aaff-4027-4b70-8cd9-http://esqsensedev01.infodati.local/sense/app/3772aaff-4027-4b70-8cd9-5d92d75a0fb6/sheet/Jswag/state... HTTP/1.1

Host: esqsensedev01.infodati.local

Connection: keep-alive

Upgrade-Insecure-Requests: 1

User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

Accept-Encoding: gzip, deflate

Accept-Language: en-US,en;q=0.8

Cookie: X-Qlik-Session=6509b2aa-fa42-4d19-99ab-79686b2637d9

HTTP/1.1 200 OK

Cache-Control: public, must-revalidate, max-age=0

Transfer-Encoding: chunked

Content-Type: text/html;charset=utf-8

Content-Encoding: gzip

Expires: Mon, 01 Jan 0001 00:00:00 GMT

Last-Modified: Wed, 10 May 2017 11:34:40 GMT

Accept-Ranges: bytes

ETag: 636300128800000000

Server: Microsoft-HTTPAPI/2.0

X-UA-Compatible: IE=edge

Date: Mon, 15 May 2017 15:22:41 GMT

Access-Control-Allow-Origin: http://esqsensedev01.infodati.local

Date: Mon, 15 May 2017 15:22:41 GMT

Access-Control-Allow-Origin: http://esqsensedev01.infodati.local

.NET SDK request:

GET /app/?reloadUri=http%3a%2f%2fesqsensedev01.infodati.local%2fapp%2f HTTP/1.1

Host: esqsensedev01.infodati.local:80

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Key: NmE2MGEwOGYtMjZhNC00YWY2LWIyNmItYWM1MzgyZWQ2YmE2

Sec-WebSocket-Origin: localhost

Sec-WebSocket-Version: 13

User-Agent: NoBrowser Windows

Origin: http://esqsensedev01.infodati.local

X-Qlik-Session: /

Cookie: X-Qlik-Session=2976e7a3-3f55-4e5c-9b7c-336a921ecb8d

HTTP/1.1 101 Switching Protocols

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Accept: rmwLxWGl/jwgGZChP4giR6Dn1P0=

Access-Control-Allow-Origin: ws://esqsensedev01.infodati.local

lindybrits
New Contributor III

Re: Sharing session between Engine API and client

Hi Fabio

Wondering if you ever cracked this? I am looking for the exact same thing, but using Node.js (via enigma.js package) to connect to the user’s session.

Kind Regards,

Lindy Brits.

chrislemm
New Contributor II

Re: Sharing session between Engine API and client

I faced your problem a while ago (now I'm having the same issue in JavaScript -.-) but I came to an solution.

Uses NTLM to authenticate, chooses whether it's desktop or enterprise. Get's the default app session.

1. Connect:

Uri absolutURI = new Uri(this.host);

this.hostLocation = Qlik.Engine.Location.FromUri(absolutURI);

if (this.isQlikSenseDesktop)

   this.hostLocation.AsDirectConnectionToPersonalEdition();

else

   this.hostLocation.AsNtlmUserViaProxy(true, null, false);

2. Open App:

this.appID = this.hostLocation.AppWithNameOrDefault("NameofyourApp", noVersionCheck: true);

if (appID != null)

{

   this.appSession = SessionType.Default;

   ISession appSession = Session.WithApp(this.appID, this.appSession);

                 

   this.app = (App)hostLocation.App(this.appID, appSession, noData: false, noVersionCheck: true);

3. Filter App:

   this.app.GetField("YourField").Select("ValueToFilter");

}

Get all filters:

a

var extendedCurrentSelection = this.app.GetExtendedCurrentSelection();

foreach (var field in extendedCurrentSelection.FieldNames)

{

     IEnumerable<NxDataPage> pages = extendedCurrentSelection.GetSelectedData(field, new[]

     {

        extendedCurrentSelection.Size(field).AsPage(100)

      });

      foreach (var dataPage in pages)

      {

                 IEnumerable<NxCell> Cells = dataPage.AllCells();

                 foreach (var tmpVal in Cells)

                 {

                      Console.WriteLine("Selected value : " + tmpVal.Text);

                 }

       }

}

If you have further questions let me know.

Greetings.

Community Browser