views:

69

answers:

3

I need a method to monitor user edit sessions, and one of the solutions I'm reviewing will have me using an unload event to send an ajax request to inform the server of the end of the edit session. (See: http://stackoverflow.com/questions/3530165/monitoring-user-sessions-to-prevent-editing-conflict)

My (rather limited) reading on the unload event indicate that the codes attached to this handler has to run quickly, and as such is usually used for clearing objects to prevent memory leaks.

My question is, can this work reliably enough for this purpose?

PS. I know about the async: false option.

A: 

You'll have to do your own testing about whether or not your particular scenario works with the time you have in unload, but making the AJAX request is pretty fast, since AJAX is asynchronous. You just send the request and then you're done! (Maybe you'll have to clear the request object you just created, though.)

If you wanted to verify that the AJAX request made it, then you'd have to worry more/use the async:false option (like this discussion indicates). But, just sending is a quick boom-and-you're-done operation.

palswim
Well, I won't call it reliable if it can't reach the server, right? ;)
Yi Jiang
True, but I thought you meant reliable in that the browser will actually send the request.
palswim
A: 

We have a case where we needed that. It's a report page that needs serious memory on the server so we wanted to free it immediately as soon as they left the page. We created a frameset and added the unload handler there. The most reliable way was to set the src of an image to the freeing script. We actually used both the unload and onbeforeunload for cross browser compatibility. It didn't work in web kit nightlies but management was OK with that.

However, that was not my proposed solution. I would use a heartbeat approach which involves more work but is much more robust.

Your page should send out periodical heartbeat requests. Each request sets the last heartbeat from a page. You then need a thread that runs on the server and clears memory if the last heartbeat was too long ago.

This doesn't solve the problem of leaving the page up for a long time. For that you need some monitoring for user activity and leave that page after a period of inactivity (make sure you confirm with the user)

Juan Mendes
Do you *really* need the `frameset` , image and freeing script method? It seems rather hackish... Or was that a lot more reliable compared to using the two `unload` events?
Yi Jiang
We needed the frameset because there was navigation going on within the page, we only wanted to send the "free-memory" request when navigating away from the set of pages. The image/two-unload method was the way we got it to run most reliably across browsers and OSs.
Juan Mendes
+1  A: 

This method is fairly reliable, if your server is fast enough to respond. Something to really watch out for though. If you close the browser and send AJAX request on unload event, there's a very good chance that the response isn't going to come back from the server in time before the window object is destroyed. What happens in this case (at least with IE) is that it will orphan your connection object and not terminate it correctly until the connection timeout is hit. If your server doesn't have connection keep-alive turned on, after you close 2 windows (while still having another window open), you will run out of open connections to the server (for IE6-7, for IE8 - 6 windows) and you will not be able to open your website until your connection timeout is hit.

I ran into a situation like that before were I was opening a popup window that was sending an AJAX request on unload, it was very reliable, but it was plagued by the issued described above, and it took really long time for me to track it down and understand what's going on. After that, what I did, is I made sure that opening window would have the same code to call server, and on every unload checked for the opener and ran the code there if it was present.

It seems that if you close the very last browser window, IE will destroy connection properly, but if one other window is open, it will not.

P.S. And just to comment on the answer above, AJAX is not really async. At least JS implementation of it isn't. After you send a request, you JS code is still going to be waiting for response from the server. It's not going to block your code execution, but since the server might take a while to response (or long enough for Windows to terminate IE window object) you might and probably will run into the problem described above.

Ilya Volodin