End a Session when User closes Browser Window via Red 'X' Button


In a Chat Session, if the User exits the Browser Window via the Red ‘X’, the User Agent is not informed of the User leaving.

This is a problem as the User Agent is then not aware that the User has left the Chat.

I am trying to call a javascript function during this close event that “cleans up” the session and informs the User Agent.

So far, I have code to detect the different browser close events…

$(window).on(‘onbeforeunload’, function ()




$(window).on(‘beforeunload’, function ()




$(window).on(“unload”, function ()




I call pageCleanup() on the close event that happens for that particular browser.

Next, I found that I must send a Synchronous AJAX (SJAX?) Request, an Asynchronous AJAX Request will not work because the Browser ends up killing the thread before it has a chance to return. A Synchronous Request of course is not ideal as it blocks the thread and can freeze the UI if there is a delay or a load on the server. It is also not possible to add a timeout for this, so you’re kind of restricted in it’s use :confused:

However, it works for me and what’s more, when I dig into the fastpath webchat code to see how they operate (I noticed when you close the User Agent window that the User IS informed) that they’re doing it the Synchronous Request way too…

See openfire_3_9_3\src\plugins\jitsivideobridge\src\apps\jitmeet\app.js

$(window).bind(‘beforeunload’, function () {

if (connection && connection.connected) {

// ensure signout


type: ‘POST’,

url: config.bosh,

async: false,

cache: false,

contentType: ‘application/xml’,

        data: "<body rid='" + (connection.rid || connection._proto.rid) + "' xmlns='http://jabber.org/protocol/httpbind' sid='" + (connection.sid || connection._proto.sid) + "' type='terminate'><presence xmlns='jabber:client' type='unavailable'/></body>",

success: function (data) {

console.log(‘signed out’);



error: function (XMLHttpRequest, textStatus, errorThrown) {

console.log(‘signout error’, textStatus + ’ (’ + errorThrown + ‘)’);






The important part here being async set to false, making it Synchronous.

Please tell me what you think of my solution, I know I have a solution here but I wasn’t sure about the Synchronous Request! Is it considered dangerous? Are there alternatives etc…

Thank you,