Domanda

I am working on a web application where I need to track time in which user is using this application.
But the question is how to capture that event when user closes tab or window ?
I found one solution that create a scheduler that runs on server side. Send a request to server every 10 minutes from client side.
So whenever server stops getting request from client side, I can assume that client has stopped using this application. That's how I can track logged in time for a particular user. Still there is one problem in this approach, logged in time may have 10 minutes difference because server is getting request from client side every 10 minutes.
This approach seems to be awkward. Is there any other approach to find logged in time in which user is accessing my web application ?

Thanks in advance

È stato utile?

Soluzione

Actually, you can not capture event like this, because there are so many scenarios.
But one way to achieve this, is to create a scheduler.
Let me explain it in detail.

  1. Create a scheduler at client side which will send a request to server.
  2. Create a scheduler at server side which will capture a request sent by client side.

Scheduler should be at some time interval.
This way you can know that your client is live. The moment you don't get any request from client side, you assume that client is disconnected.

Altri suggerimenti

You can use socket.io and persistently monitor the user/see exactly when the user closes the connection.

You can also track when the user leaves the page through the onunload event but I think this is not getting called if the user closes the tab/window instead of navigating away.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top