I am trying Redis PubSub using the Redix library but it throws the following error:
iex(1)> {:ok, pubsub} = Redix.PubSub.start_link([])
** (UndefinedFunctionError) function Redix.Utils.sanitize_starting_opts/1 is undefined (module Redix.Utils is not available)
Redix.Utils.sanitize_starting_opts([])
(redix_pubsub) lib/redix/pubsub.ex:290: Redix.PubSub.start_link/1
I am trying to get some worker processes to subscribe to channel messages: Redix.PubSub.subscribe(pubsub, "my_channel", self()). supervisor gets all the messages if I put this code in the supervisor. Anyway I can get child processes of a supervisor to receive these?
Yes. That worked. I had to store the pubsub connection object in a GenServer and then push messages to a set of workers to process. This pattern allows me to add more subscribers at any time. Here is the working code:
def init(args) do
{:ok, pubsub} = Redix.PubSub.start_link()
Sg.Subscribers.update_pubsub_link(pubsub)
DynamicSupervisor.init(strategy: :one_for_one)
end
def add_channel_reader() do
{:ok, pid} = DynamicSupervisor.start_child(@me, Sg.ChannelReader)
pubsub = Sg.Subscribers.get_pubsub_link
Redix.PubSub.subscribe(pubsub, "my_channel", pid)
end