Hi Folks,
I am working on small phoenix live view application. I have created method which will broadcast after inserting/updating and there is a live view which subscribed to that event and re-renders. If i update via postman everything works as expected. But it is not working from iex shell.
Methods in context
def update_hit_count(%ApiHit{} = api_hit, attrs) do
api_hit
|> change_api_hit(attrs)
|> Repo.update()
|> broadcast(:api_hit_updated)
end
def subscribe do
Phoenix.PubSub.subscribe(Bramble.PubSub, "users")
end
defp broadcast({:error, _reason} = error, _event), do: error
defp broadcast({:ok, user}, event) do
Phoenix.PubSub.broadcast(Bramble.PubSub, "users", {event, user})
{:ok, user}
end
Live view methods
def mount(_params, _session, socket) do
if connected?(socket), do: Accounts.subscribe()
{:ok,
socket
|> assign(:user_list, Accounts.api_hits_list()), temporary_assigns: [user_list: []]}
end
@impl true
def handle_info({:api_hit_updated, _api_hit}, socket) do
{:noreply, update(socket, :user_list, fn _user_list -> Accounts.api_hits_list() end)}
end
I am doing this in my shell, But it is not refreshing my page. But if update via postman it works.
iex(33)> api_hit = Accounts.get_api_hit(%{id: 45})
iex(34)> Accounts.update_hit_count(api_hit, %{hits: 998})