MyApp.Endpoint.broadcast vs Phoenix.PubSub.broadcast? Are they indentical?

These two function calls: MyApp.Endpoint.broadcast, and Phoenix.PubSub.broadcast, seem to do the same thing(except the endpoint is wrapped in a Broadcast struct). Are they exactly the same? Does one do something the other doesn’t? Which should I default to using?

:wave:

For me the most important distinction is MyApp.Endpoint.broadcast couples the invocation to *_web part of the app, and Phoenix.PubSub.broadcast doesn’t.

3 Likes
   MyAppWeb.Endpoint.broadcast("delivery_slots:1", "testing", %{test: "data"})

is same as

Phoenix.PubSub.broadcast(MyApp.PubSub, "delivery_slots:1", %Phoenix.Socket.Broadcast{
        topic: "delivery_slots:1",
        event: "testing",
        payload: %{test: "data"}
      })

which can be read, for example, in LiveView as:

  @impl true
  def handle_info(
        %Phoenix.Socket.Broadcast{
          event: "testing",
          payload: payload,
          topic: _topic
        } = msg,
        socket
      ) do
    IO.inspect(msg, label: "TESTING BROADCST ON EVEN TTESTING")
    {:noreply, socket}
  end
5 Likes