I’m trying to subscribe to a PubSub on a different module when I run the task which is gonna broadcast.
In the first place, I call an async task, which calls a function on a specific GenServer. That job could take 10 seconds.
I want to allow user to keep navigating the site and subscribe everywhere to that topic, waiting for the PubSub reply message.
If I stay and wait, I receive the message with the handle_info, but if I change the page, even subscribed again, the broadcast is lost on the space…
To my surprise, seems like “broadcast” only send the message to a specific process instead “broadcast” to all the process…
I’m thinking on face this with WebSockets, but I thought that PubSub maybe has something for these “persist” cases… SQS or MQ could be another option but I think that is too much…
You can broadcast on a channel, and receive on another one…
You can do it per user, or per session.
As long as users have their own custom channels, it’s possible to use it for reply. If You subscribe to this channel in each liveview, that should be fine.
def mount(_params, session, socket) do
user = Accounts.get_user_by_session_token(session["user_token"])
if connected?(socket) do
IO.puts("Subscribing to user:#{user.id}")
Phoenix.PubSub.subscribe(Coopchain.PubSub, @topic)
Phoenix.PubSub.subscribe(Coopchain.PubSub, "user:#{user.id}")
end
....
end
And on the GenServer side I should broadcast to…?
Phoenix.PubSub.broadcast(App.PubSub, "user:#{user_id}", %{
status: :error,
message: "Error on abi parser"
})
I’m not sure about following you… Thanks for your patience…
1)I subscribe on liveview.
2)I send a job to GenServer using Task Supervisor async.
3)I move to another view when I’m also subscribed.
4)I receive the PubSub message there.
broadcast is only on GenServer… The first job is triggered by a Task:
Using Task Supervisor async will link the caller, and that is not good if the caller is the liveview process… because it will die when the liveview process dies.
Thanks for your reply kokolegorille. I think that what you say is like I had before, but the GenServer call is code-blocking. That the reason I’ve settled a Task Async, to avoid the code execution blocking…