Unsubscribe on window close

33 views
Skip to first unread message

marie_dk

unread,
Jan 28, 2011, 4:18:54 PM1/28/11
to Hookbox User Group
I am developing a private chat with hookbox. When a user logs on to
the website, they are automatically subscribed to a channel called
'alert'. I am using this to alert about other users logging on an off
the site etc.

If user A wants to chat with user B, a new smaller browser window is
opened containing the chat GUI. Within that window, user A subscribes
to chat_24 which is reserved for conversations between these two
users.

The challenge for me, is to make sure that user A and user B are
unsubscribed from chat_24, if they close the chat window. As you all
know, the onunload event is not to be trusted. Some browsers simply
ignore it. There is also onbeforeonunload, but it isn't supported by
all browsers either.

If a user leaves the site all together, the server unsubscribes him
fairly quickly, but if he just closes the chat window and keeps
surfing the site, he stays connected to the chat channel, even though
the main window only subscribes to 'alert' on load.

How do I make sure, that users are unsubscribed from chat_* channels,
when the chat window is closed?

Since the chat window has a name, I thought about letting the main
window check if the chat window exists, with an interval of 20 seconds
or so. But how does the script know what channels to unsubscribe? Can
I get a list of subscribed channels from the hookbox connection?

/marie_dk

Clifford Meece

unread,
Jan 28, 2011, 4:28:15 PM1/28/11
to hoo...@googlegroups.com, Hookbox User Group
Maybe popup the chat window with a jqueryui dialog. I believe it has an on close handler.

--
Clifford Meece
312-607-5189

marie_dk

unread,
Jan 28, 2011, 6:24:26 PM1/28/11
to Hookbox User Group


On 28 Jan., 22:28, Clifford Meece <clifford.me...@gmail.com> wrote:
> Maybe popup the chat window with a jqueryui dialog.  I believe it has an on close handler.
>

I assume you mean a dhtml window/layer... That is not an option
because this kind of window will disappear when the user goes to the
next page. I need something more permanent. Furthermore the browser
would have to re-initialize the chat all over again on every request.

But I may have found a solution... The docs say:

"Its important to understand that the onSubscribed callback can be
called even if you’ve never made a call to subscribe. This might be
because the web application decided to auto_subscribe you to some
channel, or it could be because the user is already logged in and
subscribed to multiple channels, though in a different browser window
or tab. If the subscribe call is made successfully in another tab,
then this tab’s Hookbox connection object will also issue an
onSubscribed callback."

So basically the above says that every open window knows about any
subscriptions. So know I've added
setInterval('checkToSeeIfChatIsStillOpen()',10000). This function will
look for the chat window. If it does not find it, any chats are
unsubscribed.

The trouble is, that the reference to the chat window is lost, if the
user navigates in the main window... So I am still stuck... But
wondering if I can use cookies in a clever way....

/marie_dk

Rob Weiss

unread,
Jan 31, 2011, 6:20:13 AM1/31/11
to hoo...@googlegroups.com
Dojo has dojo.addOnUnload, which gets called when the users navs away from the current page. It is cross browser compliant.

Since all of the apps I have developed (recently) with hookbox/dojo are web 2.0, typically I only need one page... maybe a shift in your design paradigm is in order?

marie_dk

unread,
Mar 28, 2011, 3:17:17 AM3/28/11
to Hookbox User Group
On 31 Jan., 13:20, Rob Weiss <j105....@gmail.com> wrote:
> Dojo has dojo.addOnUnload, which gets called when the users navs away from
> the current page. It is cross browser compliant.
>
> Since all of the apps I have developed (recently) with hookbox/dojo are web
> 2.0, typically I only need one page... maybe a shift in your design paradigm
> is in order?
>

Sorry for this late reply... The website I am working on has
experienced some major performance problems.

I am already using Dojo extensively, but I never thought about using
addOnUnload function.

For now, I am using javascript to write a timestamp to a cookie every
5 seconds within the chat window. So if the stamp is older than 5
seconds, the chat window is assumed to be closed.

But I will definitely add the addOnUnload event and se how it goes...

Thanks :-)

/marie_dk

marie_dk

unread,
Apr 8, 2011, 5:32:35 PM4/8/11
to Hookbox User Group
On Mar 28, 9:17 am, marie_dk <derri...@gmail.com> wrote:
> On 31 Jan., 13:20, Rob Weiss <j105....@gmail.com> wrote:
>
> > Dojo has dojo.addOnUnload, which gets called when the users navs away from
> > the current page. It is cross browser compliant.
>
>
> I am already using Dojo extensively, but I never thought about using
> addOnUnload function.
>

The code is executed, and even though subscription.cancel() is fired,
the user stays connected and onUnsubscribe is not fired.

The same command works outside the addOnUnload event. Could it be that
all or parts of the page is already destroyed by the browser at this
point?

Either way, I can't trust it to work. I guess I just have to rely on
hookbox unsubscribing the user after 60 seconds.

/marie_dk

Andy K

unread,
Apr 10, 2011, 2:52:35 AM4/10/11
to Hookbox User Group
On Apr 9, 4:32 am, marie_dk <derri...@gmail.com> wrote:

> Either way, I can't trust it to work. I guess I just have to rely on
> hookbox unsubscribing the user after 60 seconds.
>

Could you go into more detail on how this work? Can hookbox
automatically unsubscribe idleusers? If it have something like that, I
don't think I've read it in the docs.

marie_dk

unread,
Apr 10, 2011, 3:13:57 AM4/10/11
to Hookbox User Group
If the window connected to hookbox is closed, hookbox will unsubscribe
the user after 60 seconds, and it also triggers onUnsubscribe on other
clients connected to the same channel. There is a note about it here:

http://hookbox.org/docs/javascript.html
(See the last line in the "Disconnecting" section)

/marie_dk

marie_dk

unread,
Apr 10, 2011, 3:17:31 AM4/10/11
to Hookbox User Group
And by the way... Channels are also automatically removed when all
users are unsupscribed. If the channel is moderated, this only happens
if the unsubscribe callback returns [true,{}]

/marie_dk

Andy K

unread,
Apr 10, 2011, 4:53:14 AM4/10/11
to Hookbox User Group
Thanks for pointing that out, I missed that. The reason I'm asking is
because I've seen none of that behavior on the hookbox I'm running.

Currently I rely on the browser's unload events to disconnect users,
which so far has been reliable in Chrome but not in Firefox. This
result in the number of connected user increasing over time.

function endConnection(){
subs.cancel();
conn.disconnect();
}

$(window).unload(function(){
endConnection();
});
window.onbeforeunload = endConnection;

Is the disconnect-after-60-seconds feat working in your case?

marie_dk

unread,
Apr 10, 2011, 5:55:47 AM4/10/11
to Hookbox User Group
On 10 Apr., 10:53, Andy K <a...@airslash.net> wrote:
>
> Is the disconnect-after-60-seconds feat working in your case?
>

Yes, it works every time... As long as your unsupscribe callback
returns [true,{}], or channel is not moderated.

In my latest approach, I was connecting every online user because I
wanted to send alert messages via hookbox. It works very well
actually, but since hookbox can't handle 700+ users at the moment, I
had to rethink my setup.

Now users are connected only if they open the chat window and
therefore I needed the onbeforeunload event. The code in the
onbeforeunload seems to be executed fine, but messages to hookbox
never arrives. This can mean two things, either the browser just skips
any network communication, or connections are already lost when the
code is executed.

/marie_dk

Reply all
Reply to author
Forward
0 new messages