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?
Trending in Questions
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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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:
MatijaL
No, it’s another LiveView, sorry for not mentioning it. How do I get
pidof another LiveView?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
I’ve found a post that goes into it in much more detail here: Tracking online users with Presence
slouchpie
You are trying to “live render”
but your module is called