The correct way to detect a page leaving a user

I am creating WebApp (ERP) and I need to display the people who are currently logged in and active on the page. I managed to get something pretty accurate by listening to mouse / keyboard events and periodically reporting to the database.

I don’t know how to tag people offline when they close the page. I tried to use it onbeforeunload, but it obviously fires when the user simply changes the page (click the link inside the ERP that points to another page in the ERP).

Then I tried to use WebSockets, but the problem is the same: every time the page is re-encoded, the connection is WebSocketsclosed.

Therefore, I can think of two ways:

  • Use WebSocketsand replace all links to a javascript function call, which will somehow tell the server that the user is going to change the page (so that the server does not mark it offline), but this does not seem correct , semantically speaking, the links should be links, they just indicate to another location.

  • Use either WebSocketsor AJAXnever change the page: links are replaced by a function that will call the content and display it on the screen (updating the DOM with Javascript). But again,, it doesn’t feel good either , because semantically speaking, the page would not matter and the URL will never change, so the user will not be able to “copy paste” the link to the page to link to it, correctly ?

So, is there a proper, clean way to do this? Thank you for your help.

+4
source share
3 answers

-, , - , webSocket.

, , , , , , (, ) .

, , , WebSocket , . ( , ) , , . , , , (, 10 ), , . , , , , , .

"" , Javascript, , - javascript, . webSocket , , , -, , .

+1

, , /.

, , :

{fingerprint: 123123124234, logged: true, page: home}

  1. , :

( ) 10 , .

js timer: http://www.w3schools.com/jsref/met_win_setinterval.asp

, , id - , 5 , , , .

60 . :)

+1

All Articles