acrolink
Tracking online users using Phoenix sockets and Presence?
I want to use Phoenix sockets and presence to track users who are currently connected to the Internet (from within an Android application). Is there any online tutorial explaining how to do this? Code sample? Thank you.
Most Liked
kokolegorille
Everything is done automaticaly, Presence will detect disconnection as well…
This is a simple code I put in my channels
def join("schedule", _params, socket) do
send(self(), :after_join)
{:ok, socket}
end
def handle_info(:after_join, socket) do
user = socket.assigns.user
{:ok, _} = Presence.track(socket, user.id, %{
username: user.name,
online_at: System.system_time(:second)
})
push(socket, "presence_state", Presence.list(socket))
{:noreply, socket}
end
It does imply the socket is assigned to a user, for this, i extract user from a phoenix token.
You also need a client library supporting Phoenix websocket and channels.
And this library needs to be compliant with the Phoenix version.
kokolegorille
I cannot tell for the Android client, but using the JS client, it’s almost instantly, when working on my dev machine… Which client do You use?
kokolegorille
Last Post!
kokolegorille
Yes, but I know it’s not possible to use old Phoenix JS with new version, it does not work… I think it’s the same for the Java client.
I had to write a driver for ReasonML, because using the previous was not working.
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









