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

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics 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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement