slider_channel.ex 673 Bytes
Newer Older
1
2
3
defmodule Grains.SliderChannel do
  use Grains.Web, :channel

Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
4
5
6
  alias Grains.StatefulMap
  alias Grains.ValueView

7
  def join("slider:" <> slider_id, _params, socket) do
Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
8
9
10
11
    values = StatefulMap.values()

    resp = %{values: Phoenix.View.render_many(values, ValueView, "value.json") }
    {:ok, resp, socket}
12
13
  end

14
15
  def handle_in("slider", params, socket) do

Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
16
17
18
19
20
21
    IO.puts "slider: #{params["slider"]} : #{params["value"]}"
    slider = params["slider"]
    value = params["value"]

    StatefulMap.put(slider, value)

22
23
    broadcast! socket, "slider", %{
      user: %{id: "anon"},
Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
24
25
      element: slider,
      value: value
26
27
28
29
30
31
32
33
    }

    {:reply, :ok, socket}

  end



34
end