Skip to content

Instantly share code, notes, and snippets.

@jeregrine
Created September 7, 2014 19:17

Revisions

  1. Jason S. revised this gist Sep 7, 2014. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion event_source.ex
    Original file line number Diff line number Diff line change
    @@ -5,4 +5,14 @@ def index(conn, %{"event" => "true"}) do
    {:ok, conn} = chunk(conn, ["data: ", JSON.encode!(Thermostat.Data.get()), "\n\n"])
    Phoenix.Topic.subscribe self, "data"
    data_updated(conn)
    end
    end

    defp data_updated(conn) do
    receive do
    { :update, data } ->
    IO.puts "GOT DATA"
    {:ok, conn} = chunk(conn, ["data: ", JSON.encode!(data), "\n\n"])
    _ ->
    end
    data_updated(conn)
    end
  2. Jason S. created this gist Sep 7, 2014.
    8 changes: 8 additions & 0 deletions event_source.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    def index(conn, %{"event" => "true"}) do
    conn = conn
    |> put_resp_content_type("text/event-stream")
    |> send_chunked(200)
    {:ok, conn} = chunk(conn, ["data: ", JSON.encode!(Thermostat.Data.get()), "\n\n"])
    Phoenix.Topic.subscribe self, "data"
    data_updated(conn)
    end