ryan
ryan

Reputation: 1354

cometd bayeux: handling user disconnection properly on page unload

Current setup: cometD servlet with jQuery cometd plugin.

Let's say i have a simple chat room that currently has 2 users (userA and userB) connected to it. When userB navigates away from the page, I need userA to be notified that userB has left the room. Is there a way to let userA know that userB left without using the unload handler?

$(window).unload(function(){
/** lets notify other users that currentUserId left **/

});

After conducting lots of tests; the above code block is not reliable.

The only solution i can think of is using a setInterval where every 1 minute, the admin will loop through an array of connected user, then "pings" them to check whether or not they're still connected.

Any ideas? Should i use the setInterval polling technique?

Upvotes: 0

Views: 356

Answers (2)

Christian
Christian

Reputation: 7429

Yes I know this question is old, but why not simply using the PresenceListener?

Upvotes: 0

ryan
ryan

Reputation: 1354

For this who are interested.

You can register a "removeListener" method when a server expires a user's session.

    client.addListener(new ServerSession.RemoveListener() {
        public void removed(ServerSession session, boolean timeout){
            members.values().remove(session.getId());
            broadcastMembers(members.keySet());
        }
    });

Full code example: ChatService.java#handleMembership

Upvotes: 2

Related Questions