pejrich

pejrich

What's the best pattern for a pubsub event listener? GenServer seems like the wrong choice

Let’s say I have a basic slack-like chat app. User’s can post messages, like messages, reply to messages, etc. It’s build using LiveView, so when I message is created/liked I publish a event to pubsub so the live_view can handle the UI updates. That’s all fine and working, but let’s say there are side effects, like send the user a notifcation of the liked message, or updating the user’s point score for a liked message. Or emailing the user if a message is replied to. Etc.

One solution is something like this:

defmodule Notification.EventHandler do
   def start_link(), do: ...

  def init(_) do
    Phoenix.PubSub.subscribe(MyApp.PubSub, "notification_event")
    {:ok, nil}
  end

  def handle_info({:liked_message, msg}, state) do
    create_notification_here(msg)
    {:noreply, state}
  end
end

This seems ok, because at least it’s async to the user liking the message, but now all “notification_events” (let’s say those are :liked_message and :message_reply) are handled sequentially.

One way to slightly improve is to change the handle_info to:

  def handle_info({:liked_message, msg}, state) do
    Task.start(__MODULE__, :create_notification_here, [msg])
    {:noreply, state}
  end
  
  defp create_notification(msg), do: some_stuff_here()

Now at least the time it takes to create the notification is blocking the GenServer, but each event is still being handle sequentially.

Is there a better way to concurrently handle events like this?

First 2 of 2 Posts Switch mode

kokolegorille

kokolegorille

I do have something similar… but I separate Listener from Event handler.

defmodule VideoStore.Core.Listener do
  use GenServer
  ...
  @impl GenServer
  def handle_info(command, _state) do
    EventHandlers.handle(command)
    {:noreply, nil}
  end
  ...
end

I also have more than one message type, and more than one pubsub. Not only “notification_event”, but like identity event, emailer event etc. And I have more than one Listener… I put a Listener in each bounded contexts.

For example, I have one Emailer context, that react to message identity-command register_user. It sends email when a new user register, on a given channel.

It’s async and decoupled.

al2o3cr

al2o3cr

This seems ok, because at least it’s async to the user liking the message, but now all “notification_events” (let’s say those are :liked_message and :message_reply) are handled sequentially.

This is a fact, but whether it’s a problem or not depends on your specific use case.

For instance, it provides a free guarantee that notifications are handled in precisely the order transmitted.

  def handle_info({:liked_message, msg}, state) do
    Task.start(__MODULE__, :create_notification_here, [msg])
    {:noreply, state}
  end

Be careful with patterns like this - if a LOT of messages come in at once, there’s no limit on how many processes this tries to start. Consider using something like :poolboy to get parallel workers with bounded resource consumption.

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement