chronoscope/lib/chronoscope_web/live/index_live.ex

22 lines
676 B
Elixir
Raw Normal View History

2024-05-28 18:27:02 -04:00
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