Broadcast to another live view and handle message

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!

I think the newer way of doing it is like:
Phoenix.PubSub.broadcast(:my_pubsub, topic, message)

See Phoenix.PubSub — Phoenix.PubSub v2.1.1

  1. Have you used pubsub successfully in this project before? I mean are we sure it is setup? In your application.ex did you start pubsub in your supervision?

  2. What is the return of your broadcast? Maybe you should use the bang variant broadcast!/4? In case it is not actually working and your not pattern matching on the :ok/{:errror, term()}

  1. PubSub si running. I’ve tried the same but to broadcast/subscribe using the Phoenix.PubSub methods…
  2. I’ve tried that method too. As I mentioned broadcast is received when within the same live_view but as it wouldn’t been broadcasted to another view :frowning:
    Maybe I misunderstood you.

Hi @RuslanMykulyn, I suspect that the process which needs to handle the broadcasted message has not been started or handle_event/3 is not triggered at all.

Also callback that handles some_user_interaction event needs to return {:noreply, socket} or {:reply, map(), socket}

def handle_event("some_user_interaction", params, socket) do
    MyAppWeb.Endpoint.broadcast("some_topic", "message", %{some: 'payload'})
end
1 Like

Hi, it is not the whole code. In reality it does return {:noreply, socket} and also I’m sure that the handle_event is invoked, because I have UI which is being updated accordingly :slight_smile:

Hi @RuslanMykulyn than process for company_view_live_detail.ex is not started.
What you could do is to open company_view_live_detail page in one tab and company_view_list page in another tab. In that case you have two processes that can exchange messages with each other or to handle broadcasted messages on specific topic.

1 Like
  • “Ladies and gentlemen”, we have everyone here participating :slight_smile:

Damn, I feel dumb, that’s exactly it :slight_smile: Obviously, if I don’t have an UI opened there is no connection established thus also nothing to update on UI :))

If I open two tabs then messages are being broadcasted correctly.

Thanks a lot.

1 Like

You’re welcome! Cheers :+1: