Skip to content

Instantly share code, notes, and snippets.

@adnils
Forked from jeregrine/event_source.ex
Last active August 29, 2015 14:25
Show Gist options
  • Save adnils/82b129feeb1920221fe6 to your computer and use it in GitHub Desktop.
Save adnils/82b129feeb1920221fe6 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