martin0rszulik
How to implement realtime count of registered users?
Hello, I Will be releasing my new web soon, or at least I hope
One feature that I think woud be cool to have for the release is to have a realtime count of registered users somewhere on the web
What is the best way to do this kind of thing (LiveView, Pressence or maybe Channels?) Also how expensive is it for the database If i have to run query enerytimes new user get registered, Im not expecting many users but still.
Thank you for help.
Most Liked
kokolegorille
Presence will keep track of your users… And no db is involved.
But You need to hook this to the frontend, with channels or liveview.
ImNotAVirus
Querying the database to retrieve this kind of information each time a user visits the page is, from my point of view, rarely a good idea.
Personally I would created a GenServer to store the number of registered users in its state (or ETS if you’re really looking for performance).
As soon as the GenServer is created, in a callback handle_continue/2 you make a first request to the database to get the current number of registered users and you store this as the initial state. Then at each new registrant you will call this process and ask it to increment its state.
axelson
Rather than building your own GenServer cache, I would simply use Cachex: GitHub - whitfin/cachex: A powerful caching library for Elixir with support for transactions, fallbacks and expirations · GitHub
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








