Rai Ammad Khan
Rai Ammad Khan

Reputation: 1561

Auto-detect the logout in browser tabs?

I have multiple tabs opened of a website in a browser. My question is if I logout from one window "How do other windows will detect that user has been logged out?" I am using python/django for development

The approach I am using is:

function user_checking(){

         $.ajax({
            url: /check_user_login,
            type: 'POST',

            success: function (data) {

            if (data['user_status'] == 'not_loggedin'){
            location.reload(); 
            }

            },
            error: function (data) {
            },

         }); //ajax end
    }

    var validateSession = setInterval(user_checking, 1000);

I am sending user_checking() every second to check if the user is logged out or not. Is this the right approach ?

Upvotes: 1

Views: 285

Answers (1)

Below the Radar
Below the Radar

Reputation: 7635

I think this is a perfect use case for WebSocket

The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

A you are using the Django framework, I suggest you to read this article:

https://blog.heroku.com/in_deep_with_django_channels_the_future_of_real_time_apps_in_django

If you don't want or can't use WebSocket technology, then I think your solution is not bad for a small web site.

A more lazy way could be to let the other tabs untouched until a request is made to the server. Then, the server would return a response telling that the user is disconnected.

Upvotes: 0

Related Questions