-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why present
increase when refresh the page?
#216
Comments
@Frid-Yuandu Very interesting. Thanks for reporting this issue. Can you please do us a favour: |
Sure, both two clients see the count increase. two_phoenix_counter_clients.mp4 |
Fascinating! Never seen this before. |
Could it be a cache problem? Same thing with incognito windows? Can you try to wrap the subscriptions only once the socket is connect? def mount(_params, _session, socket) do
# PubSub.subscribe(Counter.PubSub, @topic)
Presence.track(self(), @presence_topic, socket.id, %{})
initial_present =
Presence.list(@presence_topic)
|> map_size
if connected?(socket) do
PubSub.subscribe(Counter.PubSub, @topic)
CounterWeb.Endpoint.subscribe(@presence_topic)
end
{:ok, assign(socket, val: Count.current(), present: initial_present)}
end |
Ooops |
I still believe its a cache problem. Presence works with CRDT so a refresh might briefly increment but should converge. I don't have a smarter answer for this. |
I tried to clear the browser cache. After that, the current count goes down to 0, and for a short time the behavior looks normal, but keeps increasing on the next click, like each time reopen this page. Is this the cache issue you are referring to? clear_browser_cache_retry.mp4But I still don't know why others haven't encountered this🤔. Cache maybe common for browsers I think. |
I forked the code and "IT JUST WORKS"(TM)"... What about the butcher way: a complete refresh by shutting down the computer? |
Interesting, I forked and it works in my pc too :), but my code still not works even if I shutting down the pc. Will it due to the difference of the dependency 🤔? (I post my code here if needed) # in my mix.exs
defp deps do
[
{:phoenix, "~> 1.7.14"},
{:phoenix_html, "~> 4.1"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
# TODO bump on release to {:phoenix_live_view, "~> 1.0.0"},
{:phoenix_live_view, "~> 1.0.0-rc.1", override: true},
{:floki, ">= 0.30.0", only: :test},
{:esbuild, "~> 0.8", runtime: Mix.env() == :dev},
{:tailwind, "~> 0.2", runtime: Mix.env() == :dev},
{:heroicons,
github: "tailwindlabs/heroicons",
tag: "v2.1.1",
sparse: "optimized",
app: false,
compile: false,
depth: 1},
{:telemetry_metrics, "~> 1.0"},
{:telemetry_poller, "~> 1.0"},
{:jason, "~> 1.2"},
{:dns_cluster, "~> 0.1.1"},
{:bandit, "~> 1.5"},
# Track test coverage: github.com/parroty/excoveralls
{:excoveralls, "~> 0.16.0", only: [:test, :dev]}
]
end
# in tutorial mix.exs
defp deps do
[
{:phoenix, "~> 1.7.7"},
{:phoenix_html, "~> 4.0"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:phoenix_live_view, "~> 0.20.0"},
{:floki, ">= 0.30.0", only: :test},
{:esbuild, "~> 0.7", runtime: Mix.env() == :dev},
{:tailwind, "~> 0.2.1", runtime: Mix.env() == :dev},
{:telemetry_metrics, "~> 1.0"},
{:telemetry_poller, "~> 1.0"},
{:jason, "~> 1.2"},
{:plug_cowboy, "~> 2.5"},
# Track test coverage: github.com/parroty/excoveralls
{:excoveralls, "~> 0.18.0", only: [:test, :dev]},
]
end |
ah, so your code is not the repo. OK I forked it again on another computer. I changed the deps to yours. Notice you use and ** I can reproduce the bug**. One step closer. |
Thanks for pointing this out. I also tested changing Thanks again for the enthusiastic answer that helped me out! |
By the way, a "live" module should be named When I wrap the subscriptions with Please revert. Thks. def mount(_params, _session, socket) do
initial_present =
if connected?(socket) do
PubSub.subscribe(Counter.PubSub, @topic)
CounterWeb.Endpoint.subscribe(@presence_topic)
Presence.track(self(), @presence_topic, socket.id, %{})
Presence.list(@presence_topic) |> map_size()
else
0
end
{:ok, assign(socket, val: Count.current(), present: initial_present)}
end and add: def handle_info(
%{event: "presence_diff", payload: %{joins: joins, leaves: leaves}},
%{assigns: %{present: present}} = socket
) do
dbg(joins)
dbg(socket.id)
{_, joins} = Map.pop(joins, socket.id, %{})
^^^
dbg(joins)
changes = (map_size(joins) - map_size(leaves))
new_present = (present + changes) |> dbg()
{:noreply, assign(socket, :present, new_present)}
end The documentation says:
|
@Frid-Yuandu @nelsonic once checked, an update? |
yeah, we could add a section to the end of the tutorial on using |
This shows that these HTTP servers behave - surprisingly - really differently. |
Agreed. 👌 |
Aha, that's helpful |
@Frid-Yuandu thanks for your help. Can you confirm with these changes? (I mean directly copying the code above in your repo, as mine works). PR #217 |
Pardon me, do you want me to paste these two snippets and test them with bandit?
|
@Frid-Yuandu while we "have" you, quick question: how did you discover this repo? 💭 |
Wow it's been a while, I can't be sure. I think i find phoenix-todo-list-tutorial in Github homepage, and then found this repo. I've worked with some Phoenix LiveView tutorial before, but they were a bit vague and the complexity gave me a headache. Maybe that's what led me to find this project. lol |
Cool. Hope you found this tutorial useful in your |
I encounter a problem that the "connected clients" count increase each time I refresh the page. What's the reason of this issue and how could I fix it?
phoenix_counter_refresh_increase_bug.mp4
The text was updated successfully, but these errors were encountered: