Skip to content

Commit

Permalink
Make PubSub topic unique per LiveView process.
Browse files Browse the repository at this point in the history
  • Loading branch information
PJUllrich committed Jun 30, 2021
1 parent cda0e74 commit 43d0d4a
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/live_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ defmodule LiveEx do
def init(state, socket) when is_map(state) do
socket =
state
|> Map.put_new(:topic, "live_ex_topic")
|> Map.put_new(:live_ex_store_topic, "_live_ex_store_topic_#{inspect(self())}")
|> Enum.reduce(socket, fn {key, val}, socket ->
assign_new(socket, key, fn -> val end)
end)

:ok = Phoenix.PubSub.subscribe(unquote(pubsub_name), socket.assigns.topic)
:ok = Phoenix.PubSub.subscribe(unquote(pubsub_name), socket.assigns.live_ex_store_topic)

socket
end
Expand All @@ -43,7 +43,7 @@ defmodule LiveEx do
def dispatch(type, payload \\ nil, socket) when is_binary(type) do
action = %{type: type, payload: payload}

Phoenix.PubSub.broadcast(unquote(pubsub_name), socket.assigns.topic, action)
Phoenix.PubSub.broadcast(unquote(pubsub_name), socket.assigns.live_ex_store_topic, action)
end

@doc """
Expand Down

0 comments on commit 43d0d4a

Please sign in to comment.