I'm trying to create a Phoenix Channel that just contains a clock that users can subscribe to.
defmodule MyWeb.TimeChannel do
use Phoenix.Channel
def join("room:time", _message, socket) do
schedule_heartbeat()
{:ok, socket}
end
def handle_info(_, socket) do
broadcast!(socket, "update", %{time: DateTime.utc_now()})
schedule_heartbeat()
{:noreply, socket}
end
@heartbeat_ms 1000
defp schedule_heartbeat, do: Process.send_after(self(), :heartbeat, @heartbeat_ms)
end
The issue I have is when more than one client joins, I end up scheduling multiple ticks per second.
How can I just schedule one ticking clock?