I need a custom dispatcher for Phoenix PubSub in my application, but when I broadcast a message in some channel, Phoenix does not use my custom dispatcher for message delivery.
my dispatcher:
defmodule AuthGuardian.MessageDispatcher do
def dispatch(subscribers, _from, msg) do
# some logic
Enum.each(subscribers, fn {pid, _} -> send(pid, msg) end)
end
end
config.exs
:
config :auth_guardian, AuthGuardian.PubSub,
dispatcher: AuthGuardian.PoshtiDispatcher
application.ex
:
def start(_type, _args) do
children = [
# Start the PubSub system
{Phoenix.PubSub, name: AuthGuardian.PubSub, dispatcher: AuthGuardian.MessageDispatcher}
...
]
opts = [strategy: :one_for_one, name: AuthGuardian.Supervisor]
Supervisor.start_link(children, opts)
end
I know we can use Phoenix.PubSub.broadcast()
and pass our custom dispatcher directly to the broadcast but we need broadcast()
function in our channel module works with the custom dispatcher.