I suggest you update the server on some kind of timer so that the server never goes too far, knowing what state the client is in (possibly every 60 seconds when the client is active), pausing server updates when the client is not active.
Then, in your user interface, put some obvious user interface elements for Close or Stop that prompt the user to disable this path, and then update the server when you hit any of these buttons.
Then you can also hook up the unload event for the page and send one last ajax call. But this is not called in every case or is supported in all browsers, so this will be done in addition to the two previous methods.
source share