End of session detection between tabs

I am developing an AngularJS web application that can be opened in different tabs, I would like to know which one is best to detect when a user logs out on one of these tabs; I would like to open the login modality window on other tabs. I was thinking of using an event sent by the server, or tracking the existence of my cookie. I have never done anything like this, so I ask what are the pros and cons of my options, or if I am missing another smarter way to do this.

EDIT: on the server, I have a ServiceStack (C #) that supports Server-Sent Events out of the box

+6
source share
5 answers

When you use ServiceStack Server Services , you can simply send a notification when a user logs off.

First you need to detect that the User has logged out by running an OnLogout session or Auth event , for example:

public class MyAuthEvents : AuthEvents { public IServerEvents ServerEvents { get; set; } public override void OnLogout(IRequest httpReq, IAuthSession session, IServiceBase authService) { var channel = "home"; //replace with channel tabs are subscribed to var msg = "..."; ServerEvents.NotifyUserId(session.UserAuthId, "cmd.logout", msg, channel); } } 

A notification using UserId will send a notification to different tabs, as well as to browsers in which the user is logged in. If you want to send notifications only to all tabs in only 1 browser, you can use the NotifySession(session.Id) API.

To register an AuthEvents handler with a ServiceStack, you just need to register it in the IOC, for example:

 container.RegisterAs<MyAuthEvents, IAuthEvents>(); 

Then handle the cmd.logout notification in the JavaScript ServerEvents client , for example:

 $(source).handleServerEvents({ handlers: { logout: function (msg) { //Show AngularJS dialog $mdDialog.alert({ title: 'Attention', textContent: msg, ok: 'Close' }); }, //... Other custom handlers } }); 
+3
source

Events posted by the server are a pretty good way to handle this. Event-based processing is good in that it saves resources unlike other methods such as (checking for cookies) or constantly polling data or the type of heartbeat. Thus, any method in which the server can report that the client application is working on something (in your case, a pop-up message about the announced session) will do everything possible. You can also use socket.io, which is well designed and can do this easily.

+2
source

You can check some cookies or session when your tab receives focus. If you go to one tab and switch to another, the active tab receives an onfocuse event. You can check if there is another login cookie or session.

Detection If the browser tab has focus

I think you have a similar problem.

+2
source

One of the relatively simple ways to use it is to use SignalR: when a user logs out, send a message to all other sessions of the same user.

+1
source

Short answer: there is no good way.

Long answer:

There are many ways to β€œend a session,” as well as several definitions of what β€œend a session” means.

Ways to end a session include:

  • Close tab
  • Departure
  • Close the browser.
  • Force closing the browser.
  • Internet disconnect
  • Change IP Addresses
  • Power outage
  • Clear browser history

There are two commonly used solutions ...

  • Ask the client to poll the server every 30-60 seconds to say that I am alive.
  • Use websites that can detect closure.
0
source

All Articles