Possible to get current_user from Phoenix Presence or Channels?

Hi all,

I am working on a chat app and when I display the list of online users with Presence I don’t want the current user to see himself.

Any thoughts on how to do this or inside what function?

All I need really is to skip over the current user when I am rendering the list of online users, but I am not sure how to get the current user to compare it with the Presence.list.

Here’s some code that might help you visualize what I am talking about:

renderOnlineUsers(presences) {
    let onlineUsers = Presence.list(presences, (_id, {metas: [user, ...rest]}) => {
      return this.onlineUserTemplate(user)
    }).join("")
    document.querySelector("#online-users").innerHTML = onlineUsers
  },

  onlineUserTemplate(user) {
    return `
      <div id="online-user">
        <strong class="text-secondary">${user.first_name} ${user.last_name} (${user.gender})</strong>
      </div>
    `
  }

Thanks for your help.

simplest would be to chain a filter before your join, ie:

.filter(user => user.id != window.currentUserIdOrWhatever).join("")
5 Likes