Skip to content

Instantly share code, notes, and snippets.

@jeregrine
Created September 7, 2014 19:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jeregrine/8e962d04302f1869ec53 to your computer and use it in GitHub Desktop.
Save jeregrine/8e962d04302f1869ec53 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