Skip to content

Instantly share code, notes, and snippets.

@tlvenn
Forked from jeregrine/event_source.ex
Created October 29, 2016 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlvenn/0e2e5785e7b7039c61525f407d357abe to your computer and use it in GitHub Desktop.
Save tlvenn/0e2e5785e7b7039c61525f407d357abe to your computer and use it in GitHub Desktop.
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment