martin0rszulik

martin0rszulik

Hello, I Will be releasing my new web soon, or at least I hope :sweat_smile: 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.

Showing Posts 1 to 7

kokolegorille

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

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

axelson

Scenic Core Team
ImNotAVirus

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

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

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_copies to prevent disk writes and ensure speed.

This is a really good resource on mnesia and how to get it setup.

Best of luck.

martin0rszulik

martin0rszulik OP

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 :slightly_smiling_face:

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews