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 1 to 5

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.

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

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

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

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
RemyXRenard
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
samoloth
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
FlyingNoodle
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 Top

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
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews