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?