3

My Main GOAL and actual scenario: If two users share the same credentials, then the second user can not login into the system until the first one who is already logged in, log outs system.

Actually I want to set login_flag=0 on server database, when user closes the browser window. I googled and figured out to send an ajax request before either by

$(window).on('unload', function(){ // ajax here with async:false });

or

$(window).on('beforeunload', function(){ // ajax here with async:false });

It's working fine. But in Chrome, it logs the following message:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.

I have googled again for the same and came to know that it is discouraging to use Ajax on unload event, and found following details from here

enter image description here

in short, they are against the use of ajax with async:false with window.unload or window.beforeUnload method. If I remove async param from Ajax, then it's not working as intended.

What is the right approach if I want to do some activity on server side if user closes browser window?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Rajan Rawal
  • 6,171
  • 6
  • 40
  • 62
  • Not sure there is any proper way to make an action when the user closes the tab/window. In your case, if I understood well what you are trying to do, you might want to have a "last_activity" datetime and update regularly this time. So that you can know if user was (or was not) active the last X minutes, and consider him online or offline. –  Feb 25 '15 at 15:38
  • 2
    You can't reliably detect when a user has closed a browser window. There are all sorts of factors that could result in the request not making it to the server. You're better having some sort of state that's kept alive by user activity on the server, when you don't hear from the user for a certain amount of time, then log them out – James Thorpe Feb 25 '15 at 15:38
  • As i said earlier, please go through the question completely. before marking it duplicate. – Rajan Rawal Feb 25 '15 at 17:25

1 Answers1

0

I remember to be in a similar situation once where I needed to perform an action on server side once user closed the window. However, my research came to same conclusion as yours that is discouraged to use any type of ajax call on window unload events. So, my final approach was to set a specific Cookie on client side in the window unload event, and when the same user visited that page, only then I performed my required action and deleted the cookie from server side.

I know this is not exactly performing the action at the same time but that was the best approach which worked for me.

Omer Arshad
  • 520
  • 7
  • 16
  • I have added more details in question. If you can help me out. – Rajan Rawal Feb 25 '15 at 17:34
  • The only best solution I see for this (as I have seen on many sites) that when same user tries to login from different location, they are given warning that same credentials are being used to login from a different location and if you want to continue then same user from other location will be logged out, and hence once accepted you can easily log user out from other session. – Omer Arshad Feb 26 '15 at 12:06
  • Oh! Thats cool. How did you achieved this? Can you please throw some light on the steps to achieve it or some reference link? Thanks! – Rajan Rawal Feb 27 '15 at 05:23
  • I assume you using PHP as your backend. You can check [this](http://stackoverflow.com/questions/5443355/remotely-destroy-a-session-in-php-user-logs-in-somewhere-else) answer on similar topic. – Omer Arshad Feb 27 '15 at 11:03