tmbb

tmbb

I was thinking that it would be cool to notify clients in real time when something changes in the Repo. My question is: where should I put the notification code? The controller or the context?

If I put them inside the context I’m sure all Repo changes will be captured and will generate notifications (this is probably not a good idea, but let’s assume it is). But this couples the context to the web part of my application, which is undesirable, right?

If I put them inside the controller, I keep the separation between the web part and the context, but I can’t be sure I’ve captured all changes.

Should I have a NotificationManager module that tests whether if the web application is running before sending a notification? Is this level of “uncoupling” enough?

I’m not sure I’ll ever want to do this in production, so this is mainly a theoretical question about design.

Showing Posts 5 to 1

tmbb

tmbb OP

Thanks, that’s what I thought too.

I can see some advantages in doing this and must investigate further. Thanks :slight_smile:

I’m aware of that. That would require having the DB itself notifying my code. I’m planning on only interacting with the DB using ecto, so that’s something I’m not interested in.

bobbypriambodo

bobbypriambodo

Indeed, yep, your solution would scale better than mine. Using dedicated message queues or process should be better in the long run…

NobbZ

NobbZ

I do not think that the “context” should know who is interested in updates. It should just know that there might be someone.

If the context itself were iterating all the possible receivers each time there is an update, it could take quite a long time and massively influence the speed of database access.

Therefore I opt for a single “relay” as described in my post, but aside of that our ideas are similar.

bobbypriambodo

bobbypriambodo

Just abstract thinking here (since I’m currently at work :smile:), I would think this is a perfect use case for Pub-sub pattern. As we know, the data of your app is handled inside its own contexts outside of the web layer. How I imagine it is that you have a context that exposes a subscribe_to_updates function. The client (in this case, your web layer or something) calls that function on start and provide a handler (callback?) specifying what to do when it receives an update notification.

Everytime there’s an update, the context will iterate to the registered subscribers and call their respective handlers. This way you can decouple the subscriber from your context.

Now, how to implement that in Elixir seems to be an interesting challenge ^^

NobbZ

NobbZ

Sine I’m on mobile only brief…

If something generates an update message it should be the context or even closer to the repo. It will always emit a message then to a genserver, which then relays this message to any topic, beam process or message queue or whatever you want and has subscribed in any way. Perhaps even layered through a tree like structure of processes.

This way you’ll get also changed made by non web parts of your application. But you still won’t catch changes that were made directly in the db.

— 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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
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
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews