blksheephw

blksheephw

LiveView + PubSub topics with GenServer to keep state for each

hey all! I’ve followed an example online on how to create a simple “counter” program with LiveView where changes are reflected for all clients. (GitHub - dwyl/phoenix-liveview-counter-tutorial: 🤯 beginners tutorial building a real time counter in Phoenix 1.6.2 + LiveView 0.16.4 ⚡️)

The LiveView mounts, renders, and handle_events and the state is moved out of LV and into GenServer. A utility function inside the GenServer broadcasts the last count to the PubSub topic.

I now want to create multiple counters where each has it’s own count.
If (in theory) i could generate a slug that represents the topic for each each counter and broadcast/subscribe to that topic, how can this be done being that the topic is being set by a topic() fn inside the GenServer.

I think my question is probably due to a misunderstanding of GenServers and LV but it seems that a single GenServer would not be able to keep state for all the counters? Does the name of each GenServer need to be unique as well? How can we set the name first of all (if this is even necessary) and then also “dynamically” subscribe to each topic.

Im thinking a GenServer must be started with start_link inside the LV and the custom counter-id can be passed in but not sure if this would be right/necessary.

thank you!!!

Most Liked

msimonborg

msimonborg

I am relatively new to Elixir/Phoenix/LiveView, having only built a few small projects, but I had free time and thought this would be an interesting problem to tackle for the sake of my own learning experience with LiveView. This repo has a working implementation if you want to clone it and try it out : )

If anyone with more experience reads this and has feedback or criticism on my approach, I would welcome it!

msimonborg

msimonborg

Are you beginning with a predetermined number of counters, or are you wishing for new counters to be created and destroyed dynamically by user action?

Either way, you would most likely want a separate GenServer for each topic to isolate the state of each counter, and you would want to start your count servers under a supervision tree, rather than calling start_link directly from any LiveView.

blksheephw

blksheephw

wow you’re fast! i’ve ran it and that did the trick, thanks a million it’s blows my mind how simple it is to do these kinds of things with elixir!

msimonborg

msimonborg

My pleasure! If you have any questions about anything in there, let me know. Elixir really is a joy to use when you start getting familiar with the language features and OTP. Cheers!

Where Next?

Popular in Questions Top

logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement