jam

jam

Ephemeral state – Presence or PubSub?

What’s the canonical way to handle ephemeral state? Let’s take typing indicators as an example, e.g. Bob is typing.... At first I thought Phoenix.Presence would be perfect for this, just add a typing boolean in the metadata along with some debouncing, but then I started second guessing and thought maybe it’s not the best tool for the job. Maybe it’s better to just use Phoenix.PubSub and broadcast_from for ephemeral state. How do you think about this? Is there a good rule of thumb for choosing one vs the other?

Most Liked

rhcarvalho

rhcarvalho

While both are possible, I’d start with Presence.

Remember to clear the typing flag on blur/timeout/message sent. Presence handles user leaving the page, etc, automatically through the Process lifetime.

siquu

siquu

The rule of thumb seems to be a matter of state lifetimes. For Phoenix.Presence where the prime example is showing whether someone is “in a lobby” or “editing the same document”, the lifetime of that state is tied to the session/document process. In the case of a typing indicator, I think it could also be a contender for using Presence.

The main difference between PubSubs and Presence is that Presence conveniently holds onto relevant state that new users would need through the presence_state initialization, usually about other users. You want to list all the other users in the document, for example, when a new user joins. And if someone joins a room, you’d likely want to show someone is already typing.

In short I think Phoenix.Presence makes sense when you have long lifetimes of state and the state isn’t rapidly changing or the state is required to make sense of the application when new users join. But if the lifetime is short, or if the state changes a lot, or it’s “non-essential” info, it’s probably better to not store it and instead depend on the event stream to “synchronize” the client. For example, I have an application which shares cursor state/highlighting — because this is so rapidly updated and non-essential, it doesn’t make sense to use Presence for it.

Where Next?

Popular in Discussions Top

jswny
I would like to better understand what the advantages/disadvantages of umbrella applications are compared to structuring your app as as s...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement