diff --git a/lib/chronoscope_web/live/index_live.ex b/lib/chronoscope_web/live/index_live.ex new file mode 100644 index 0000000..d92d539 --- /dev/null +++ b/lib/chronoscope_web/live/index_live.ex @@ -0,0 +1,21 @@ +defmodule ChronoscopeWeb.IndexLive do + use ChronoscopeWeb, :live_view + + @topic "test" + + def mount(_params, _session, socket) do + ChronoscopeWeb.Endpoint.subscribe(@topic) + ChronoscopeWeb.Endpoint.broadcast_from(self(), @topic, "", %{temperature: 100}) + {:ok, assign(socket, :temperature, 100)} + end + + def handle_event("inc_temperature", _params, socket) do + updated = socket.assigns.temperature + 1 + ChronoscopeWeb.Endpoint.broadcast_from(self(), @topic, "", %{temperature: updated}) + {:noreply, assign(socket, :temperature, updated)} + end + + def handle_info(%{topic: @topic, payload: state}, socket) do + {:noreply, assign(socket, state)} + end +end diff --git a/lib/chronoscope_web/live/index_live.html.heex b/lib/chronoscope_web/live/index_live.html.heex new file mode 100644 index 0000000..c938938 --- /dev/null +++ b/lib/chronoscope_web/live/index_live.html.heex @@ -0,0 +1,6 @@ +
+ Live! +
+ Current temperature: <%= @temperature %>°F + +
diff --git a/lib/chronoscope_web/router.ex b/lib/chronoscope_web/router.ex index 75f0d25..a6b3e48 100644 --- a/lib/chronoscope_web/router.ex +++ b/lib/chronoscope_web/router.ex @@ -14,6 +14,12 @@ defmodule ChronoscopeWeb.Router do plug :accepts, ["json"] end + scope "/test", ChronoscopeWeb do + pipe_through :browser + + live "/", IndexLive + end + scope "/", ChronoscopeWeb do pipe_through :browser