1

I'm developing a web application. The front end is with Angular and back-end is a Rest API with django rest framework. For some customer requirements, I need that the django rest api is able to differentiate between polling requests with a sessionid. After some searches I found that I can identify the session in the REST API side with the value of:

request.session.session_key

But that isn't the best choice, because for two different tabs I get the same session_key and the desired sessionId that the rest api receives in the request should identify session for specific tab. So not sessionid for different PCs and host IP address, or browsers only, but also it should get two different sessionids for two different tabs.

Does django request object contain other id to identify host tabs?

I guess that the solution can be found in Angular side.

Is there a way in Angular and typescript to identify tabs sessions?

Kallel Omar
  • 1,208
  • 2
  • 17
  • 51
  • 1
    Possible duplicate of [Any way to identify browser tab in JavaScript?](https://stackoverflow.com/questions/11896160/any-way-to-identify-browser-tab-in-javascript) – insertusernamehere Sep 14 '19 at 08:05

1 Answers1

1

I don't know if you can find in some browser js variables a related tabid value, but you can create it using localStorage and sessionStorage. with local Storage you can create variables that their values are viewed by whole tabs. And sessionStarage is used to create items tha value viewed only in actual tab. we will use localStorage to store the last affected tab id and we will use sessionStorage to store the actual tabid value like that:

let lasttabid= localStorage.getItem("lastTabId");
    let tabid= sessionStorage.getItem("tabId"); 
    let lasttabidint: number;
    console.log("lasttabid: "+lasttabid+" tabid: "+tabid);
    if(tabid === undefined || tabid == null || tabid.length<=0 || !tabid){
        if(lasttabid === undefined || lasttabid == null || lasttabid.length<=0 || !lasttabid){
            sessionStorage.setItem("tabId", "1");
            localStorage.setItem("lastTabId", "1");
            tabid= "1";
        } else {
            lasttabidint= +lasttabid;
            sessionStorage.setItem("tabId", String(++lasttabidint));
            localStorage.setItem("lastTabId", String(lasttabidint));
            tabid= String(lasttabidint);
        }
    }

console.log("actual tab id is : "+ tabid);

your desired result is tabid value.