elbasti

elbasti

Can a Supervisor and a child Genserver subscribe to the same topic?

Hi folks, looking for some very basic beginner-level advice on architecture OTP applications.

I’m building a hobby-project application that receives external messages and then replies to them, and the replies depend on the conversation our application has had with that particular recipient.

The messages are received by a Phoenix application, and I’d like for a separate OTP application to be responsible for determining if/how to reply. The architecture I have in mind is:

  1. A DynamicSupervisor subscribes to a pubsub topic, :inbound_message_received.
  2. Upon receipt of the message, the supervisor does one of two things:
    a. Start a child GenServer that will then load the conversation we’ve had with that recipient as state, and then determine if/how to reply.
    b. Find an existing GenServer corresponding to that conversation, and then let it know a new message has arrived.

A few constraints:

  • Conversations are “bursty.” Sometimes there will be lots of back and forth, in which case it makes sense to keep the child servers alive, but they also go dormant, in which case it makes sense to kill them after a while until a new message is received.
  • The work done by the child servers can take a long time.

So the very first question I have is:

It seems kind of silly to have the supervisor listen to all new messages only to delegate them to the child servers that may or may not already exist. Naively, I think the children should subscribe to that topic as well–that way they can respond immediately if they already exist.

¿How would you handle that? ¿Can both a supervisor and a child of the supervisor subscribe to the same PubSub topic?

Marked As Solved

jswanner

jswanner

I wouldn’t use a supervisor to listen to all messages and route them accordingly, as that supervisor will become a bottleneck.

Instead, I would write a function that was called from the client process (your Phoenix controller/liveview process) that was responsible for looking up the pid (or starting a new process) and calling it. That look up code would use something like GenServer.whereis with a :via tuple. With that setup, I wouldn’t need PubSub for the handling of incoming messages, but I would likely use PubSub for broadcasting out from the GenServer that a message had been received and processed.

Also Liked

kokolegorille

kokolegorille

Yes, something like this…

  def get_worker(name) do
    case WorkerSup.start_worker(name) do
      {:ok, worker} -> worker
      {:error, {:already_started, worker}} -> worker
      _response -> nil
    end
  end

This is what I meant to lookup via a Registry…

jswanner

jswanner

There’s a difference between separation of business logic concerns and separation of processes. Modules are for organizing functions, processes are for runtime concerns.

No, I don’t think that would be necessary. There’s already multiple client processes (controller/LiveView processes), by doing the process lookup from them the supervisor won’t be a bottleneck (the registry might, but I wouldn’t worry about that until you can prove it’s a problem). I think you want to focus more on how the code is organized into modules more than anything

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement