MatijaL

MatijaL

Hi guys,

I’m building a small app where I would like to have a page with listed users who are currently online across the app, no matter which page they are on at the moment.

I created a LiveView to track users and the page where the list of users is showing, but if new users sign in or sign out, the list is not being updated and I get the following message in terminal:

[debug] send_update failed because component MyAppWeb.PagesLive.OnlineUsersComponent with ID "online-users" does not exist or it has been removed

app.html.heex - I added LiveView inside app.html.heex

<div>
  <%= live_render(@socket, MyAppWeb.OMyAppnlineUsersLive, id: "online-users", sticky: true) %>

  <main>
    <%= @inner_content %>
  </main>
</div>

OnlineUsersLive LiveView

defmodule MyAppWeb.OnlineUsersLive do
  use MyAppWeb, :live_view
  alias MyAppWeb.Presence
  alias MyApp.{Accounts, Profiles}
  alias MyAppWeb.PagesLive.OnlineUsersComponent

  @impl true
  def render(assigns) do
    ~H"""
      <div class=""></div>
    """
  end

  @impl true
  def mount(_params, %{"user_token" => user_token} = _session, socket) do
    current_user = Accounts.get_user_by_session_token(user_token)
    profile = Profiles.get_profile!(current_user.id)

    if connected?(socket) do
      {:ok, _} = Presence.track_user(self(), current_user, profile)

      Phoenix.PubSub.subscribe(MyApp.PubSub, "online_users")
    end

    {:ok,
      socket
      |> assign(:current_user, current_user),
      layout: false
    }
  end

  def mount(_params, _session, socket) do
    if connected?(socket) do
      Phoenix.PubSub.subscribe(MyApp.PubSub, "online_users")
    end
    {:ok,
      socket
      |> assign(:current_user, nil),
      layout: false
    }
  end

  @impl true
  def handle_info(%{event: "presence_diff"}, socket) do
    send_update(OnlineUsersComponent, id: "online-users")

    {:noreply, socket}
  end
end

presence.ex

defmodule MyAppWeb.Presence do
  use Phoenix.Presence, otp_app: :myapp, pubsub_server: MyApp.PubSub

  alias __MODULE__

  def track_user(pid, user, profile) do
    Presence.track(
      pid,
      "online_users",
      user.id,
      %{
        username: profile.username,
        name: profile.name,
      }
    )
  end
end

Online Users Component

defmodule MyAppWeb.PagesLive.OnlineUsersComponent do
  use MyAppWeb, :live_component

  alias MyAppWeb.Presence

  @impl true
  def render(assigns) do
    ~H"""
      <div id={@id}>
        Online Users
        <%= for {user_id, %{metas: [data]}} <- @online_users do %>
          <%= user_id %>
          <%= data.username %>
        <% end %>
      </div>
    """
  end

  @impl true
  def update(assigns, socket) do
    {:ok,
      socket
      |> assign(assigns)
      |> assign(:online_users, Presence.list("online_users"))
    }
  end
end

browse.ex page where signed in users are listed

<div>
Browse Users

<.live_component
  module={MyAppWeb.PagesLive.OnlineUsersComponent}
  id="online-users" 
/>
</div>

Can anyone help?

Showing Posts 1 to 5

muelthe

muelthe

Are you running the component in the same Liveview? Reading through the post I’m not 100% sure (or I’ve missed something), but if you check Phoenix.LiveView — Phoenix LiveView v1.2.5 specifically, the following:

The pid argument is optional and it defaults to the current process, which means the update instruction will be sent to a component running on the same LiveView. If the current process is not a LiveView or you want to send updates to a live component running on another LiveView, you should explicitly pass the LiveView’s pid instead.

MatijaL

MatijaL OP

No, it’s another LiveView, sorry for not mentioning it. How do I get pid of another LiveView?

muelthe

muelthe

I’ve just started working on some presence tracking myself and whilst I’ve only been looking through the documentation at this stage, it might be worth looking into Phoenix Channels and PubSub i.e. have your target liveview subscribed to a topic that receives broadcasts of updates.

There’s some useful information here that I’ve been using to get myself started: Channels — Phoenix v1.8.8

muelthe

muelthe

I’ve found a post that goes into it in much more detail here: Tracking online users with Presence

slouchpie

slouchpie

You are trying to “live render”

  <%= live_render(@socket, MyAppWeb.OMyAppnlineUsersLive, id: "online-users", sticky: true) %>

but your module is called

defmodule MyAppWeb.OnlineUsersLive do
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews