kasvith

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

zorn

From the docs, right under the sample you shared:

In the example above, Presence.track is 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

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

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement