Skip to content

Instantly share code, notes, and snippets.

@cibernox
Created April 28, 2013 19:47
Show Gist options
  • Save cibernox/5478147 to your computer and use it in GitHub Desktop.
Save cibernox/5478147 to your computer and use it in GitHub Desktop.
# I want to create an action that returns a chunked response, waiting 1 second between chunks.
# Honestly, I don't understand a single word of the documentation.
# Thats my code
defmodule ApplicationRouter do
use Dynamo.Router
prepare do
# Pick which parts of the request you want to fetch
# You can comment the line below if you don't need
# any of them or move them to a forwarded router
conn.fetch([:cookies, :params])
end
defp on_wake_up(msg, conn) do
conn.assigns[:parent] <- :awaken
conn.chunk "Hello\n"
end
defp await_wake_up(pid) do
pid <- "hello"
receive do
:awaken -> true
after
1000 -> await_wake_up(pid)
end
end
get "/story1/play" do
conn = conn.assign(:parent, self())
conn = conn.send_chunked(200)
pid = spawn_link fn ->
hibernate conn, fn(msg, conn) ->
{ :ok, conn } = on_wake_up(msg, conn)
end
end
await_wake_up(pid)
conn
end
end
# When I send a curl request I get the output in the curl_output.txt file
# My final goal is to create an action that reads a .txt file containing an story
# and chunks each line of the text file with a second between them.
# As you see in the output, I don't know how to create a loop, I only get one
# chunk. I'm absolutely lost.
$ curl -i http://localhost:4000/story1/play
HTTP/1.1 200 OK
transfer-encoding: chunked
connection: keep-alive
server: Cowboy
date: Sun, 28 Apr 2013 19:40:39 GMT
cache-control: max-age=0, private, must-revalidate
Hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment