Hello gents,
I’m trying to refresh my Elixir/Phoenix knowledge after some time.
Currently I’m struggling a couple of days with this issue.
I have a live view structure to handle list-detail UI such as this:
# router file
...
scope "/", MyAppWeb do
pipe_through [:browser_auth, :protected]
live_session :defauly,
on_mount: [{MyAppWeb .CurrentUser, :assign_current_user}] do
live "/companies", CompanyViewLive.List, :index
live "/companies/:id", CompanyViewLive.Detail, :index
live "/companies/:id/modal", CompanyViewLive.Detail, :modal
live "/investments", InvestmentViewLive.List, :index
end
end
What I want to do is, based on the user interaction on the detail view I need to broadcast a message and handle it in the list view to update the ui.
My current code goes as:
#company_view_live_detail.ex
def mount(%{"id" => id}, assigns, socket) do
if connected?(socket) do
MyAppWeb.Endpoint.subscribe("some_topic")
end
....
end
def handle_event("some_user_interaction", params, socket) do
MyAppWeb.Endpoint.broadcast("some_topic", "message", %{some: 'payload'})
end
and in the list view
#company_view_list.ex
def mount(_params, assigns, socket) do
if connected?(socket) do
MyAppWeb.Endpoint.subscribe("some_topic")
end
...
end
...
def handle_info(payload, socket) do
IO.inspect("List")
{:noreply, socket}
end
...
However no message is being handled in the list view.
When I tried to handle message in the detail view such as:
#company_view_live_detail.ex
def handle_info(payload, socket) do
IO.inspect("Detail")
{:noreply, socket}
end
then I get the “Detail” in the console.
I’ve tried everything. Any ideas why it does not work, would be highly appreciated.
PS: I linked also my router file, because maybe my “architecture” of pages is not correct or live_session is doing something?
Thanks in advance!