From f76b72538864b4f4ff6468d53b32ebc2e0033ac4 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Wed, 5 Jun 2019 19:16:21 +0100 Subject: [PATCH] =?UTF-8?q?FAIL:=20unable=20to=20get=20LiveView=20Broadcas?= =?UTF-8?q?t=20to=20send=20inc/dec=20event=20...=20=F0=9F=98=9E=20need=20t?= =?UTF-8?q?o=20go=20back=20to=20basics=20and=20follow=20the=20docs=20from?= =?UTF-8?q?=20scratch=20to=20understand=20this!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.exs | 1 + lib/live_view_counter_web/live/counter_live.ex | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/config/config.exs b/config/config.exs index a98a11ca..a37bad0e 100644 --- a/config/config.exs +++ b/config/config.exs @@ -12,6 +12,7 @@ config :live_view_counter, LiveViewCounterWeb.Endpoint, url: [host: "localhost"], secret_key_base: "oq1rRRs+4fqLuaBxIDEbrV4ALaCpglx6aEZZGKi1JiPuQt1dKKvMww2ghfxGWFFW", render_errors: [view: LiveViewCounterWeb.ErrorView, accepts: ~w(html json)], + # https://elixirschool.com/blog/live-view-with-pub-sub/ pubsub: [name: LiveViewCounter.PubSub, adapter: Phoenix.PubSub.PG2] # Configures Elixir's Logger diff --git a/lib/live_view_counter_web/live/counter_live.ex b/lib/live_view_counter_web/live/counter_live.ex index 98b6f930..3b553a94 100644 --- a/lib/live_view_counter_web/live/counter_live.ex +++ b/lib/live_view_counter_web/live/counter_live.ex @@ -1,6 +1,8 @@ defmodule LiveViewCounterWeb.CounterLive do use Phoenix.LiveView + @topic "live" + def render(assigns) do ~L"""
@@ -12,14 +14,22 @@ defmodule LiveViewCounterWeb.CounterLive do end def mount(_session, socket) do + LiveViewCounterWeb.Endpoint.subscribe(@topic) + IO.inspect(socket, label: "mount") {:ok, assign(socket, :val, 0)} end def handle_event("inc", _, socket) do + # IO.inspect(socket, label: "inc") + # IO.inspect(update(socket, :val, &(&1 + 1)), label: "update") + # broadcast(socket, "shout", Map.put_new(payload, :id, msg.id)) + + # LiveViewCounterWeb.Endpoint.broadcast_from(self(), @topic, "totes", {:val, &(&1 + 1)} ) {:noreply, update(socket, :val, &(&1 + 1))} end def handle_event("dec", _, socket) do + IO.inspect(socket, label: "dec") {:noreply, update(socket, :val, &(&1 - 1))} end -end \ No newline at end of file +end