Skip to content
Advertisement

Php Show Online Users with Pusher or socket.io

I have a forum page. On the only index page, I show an online user list that shows usernames. I use Pusher. It works great. I have a problem about which my customer is complaining. The problem is that when a user browses to other pages, his/her username disappears and re-appears again. His/her connection to Pusher is lost on every page move. How can I handle this situation? Is there any other solution to list online users? I don’t want to use a classic AJAX-based solution.

Advertisement

Answer

An example solution for this can be found on the Pusher Support page. They suggest you buffer user offline events and discard if the user rejoins after a short amount of time. The example code provided is:

function removeMember(member) {
    pendingRemoves[ member.id ] = setTimeout(function() {
      removeMemberFromUI(member);
    }, 3000); // wait 3 seconds
};

function addMember(member) {
  var pendingRemoveTimeout = pendingRemoves[member.id];
  if(pendingRemoveTimeout) {
    // user left, but has rejoined
    clearTimeout(pendingRemoveTimeout);
  }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement