kasvith
Tips on understanding Phoenix Presence?
i dont understand how phoenix presence works
in the examples
defmodule MyAppWeb.MyChannel do
use MyAppWeb, :channel
alias MyAppWeb.Presence
def join("some:topic", _params, socket) do
send(self(), :after_join)
{:ok, assign(socket, :user_id, ...)}
end
def handle_info(:after_join, socket) do
{:ok, _} = Presence.track(socket, socket.assigns.user_id, %{
online_at: inspect(System.system_time(:second))
})
push(socket, "presence_state", Presence.list(socket))
{:noreply, socket}
end
end
it pushes to presence_state on :after_join event…
after that how does it work? how state is being pushed down to client in realtime as changes are happening?
is there any good way to hook into presence_state to identify which clients are online realtime?(I want to implement a total user counter)
PS: Im following this for my usecase https://medium.com/@alvinlindstam/phoenix-presence-for-social-networks-5fb67143f0ad
Most Liked
zorn
From the docs, right under the sample you shared:
In the example above,
Presence.trackis used to register this channel’s process as a presence for the socket’s user ID, with a map of metadata. Next, the current presence information for the socket’s topic is pushed to the client as a"presence_state"event.
Emphasis above added by me. As you call Presence.track/3, it will associate the calling process with this specific user_id, and then when the process goes away (ie: the channel of the example is closed, or in other spaces the LiveView process is ended) the “presence” of this user_id is considered lost.
You might get value from this blog post, which talks about Presence at the end:
Good luck!
Last Post!
PaulOlukayode
Presence Tracking Workaround with Phoenix
kasvith said:
def join("some:topic", _params, socket) do send(self(), :after_join) {:ok, assign(socket, :user_id, ...)} end
Sometimes you don’t need to strictly tie yourself to the Phoenix Channel’s join/3 callback for presence tracking or messaging.
Workaround idea
Instead of doing everything inside join/3, you can handle presence and messages directly in your GenServer or process by sending messages: all you need is the pid either socket or genserver then bind to it. that is all
def handle_info({:send_text, text}, state) do
# Relay the message to the process/topic bound to presence
# Presence is tied to the process (pid or atom) representing the topic
end
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









