Search code examples
cometd

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?


Solution

  • 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