martin0rszulik
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.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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/2you 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
ImNotAVirus
This is also what I use when I need a cache that has to manage a large number of keys/values or when I need more advanced features such as key expiration etc…
But I figured since the author was just trying to manage a single value and increment it, it might be a bit overkill to use an dependency for that.
That’s why I recommended a simple GenServer
rjk
I read/interpret your requirement as you want to show how many users are registered, meaning the amount of records you have in your database of users registered. If that’s the case it’s perfectly valid to just do a standard SQL query on your database and count the records every page view. Most databases don’t break a sweat.
The idea behind it would be; Make it work, Make it right, Make it fast. In that order. (I believe this is coming from Kent Beck). I guess you can also add; while making it work, keep it as simple as possible at first.
A database can do 100s of queries per second, most of the time it can even keep your query ‘hot’ in memory as if you’re using a key value store like redis. If it actually becomes a problem you can make it fast by caching. But now you have another problem; cache invalidation, because if you have 2 app servers you have to coordinate the cache invalidation.
Finally when making it fast, please measure! (i have just replaced a very “fast” piece of code in ETS replaced with normal elixir maps which seem to be faster after actually measuring)
If the above interpretation of your question is not correct and the question is more about ‘live’ users visiting your website then it would be better to directly go for presence as this is better suited for that use case.
Have fun while coding!
(EDIT: just to give you an idea, look at these numbers: TechEmpower Framework Benchmarks they show you how fast a DB can be on top of beefy hardware. And it’s not even that expensive you can rent this kind of hardware with ovh or hetzner for around 70-100 usd per month.)
mpope
I think a combo of Presence (which might be too heavy, could just use a normal Channel) and a shared mnesia schema is a good way to go. It has the benefit of being consistent across the nodes if you have clustering already setup. Its not perfect, due to net splits and other consistency issues, but it can be done ‘easily’ using dirty_update_counter/3 on connection setup / cleanup. You can run mnesia using
ram_copiesto prevent disk writes and ensure speed.This is a really good resource on mnesia and how to get it setup.
Best of luck.
martin0rszulik
After some research I also come to this conclusion. Just simple count on user database is enough. I Will show it on registration template. Thanks for asistance