How Can I Stop Channels Users Going Offline For An Instant When They Navigate Between Pages?

Presence Channels are a simple way to be notified when a channel member comes online or goes offline. However, sometimes the user might refresh the page or navigate to a new part of your app - in these flows it doesn't necessary make sense to immediately mark the user offline and then put them back as online a moment later.

To overcome this, when a user leaves the room you could start a timeout to remove the user. If the user then rejoins the room within the timeout period you could clear that timeout to prevent marking the user as offline.

Code below for example purposes only:

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);
  }
}

Still have questions? Please reach out to our Support team by visiting this page.

Last updated