Created
March 23, 2014 19:09
-
-
Save eezis/9728126 to your computer and use it in GitHub Desktop.
Simple Streaming with Elixir Plug.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyPlug do | |
import Plug.Connection | |
def init(options) do | |
# initialize options | |
options | |
end | |
def call(conn, _opts) do | |
conn = send_chunked(conn, 200) | |
{ :ok, conn } = chunk(conn, "HELLO\n") | |
{ :ok, conn } = chunk(conn, "WORLD\n\n") | |
{ :ok, conn } = chunk(conn, "This is the third line\n") | |
{ :ok, conn } = chunk(conn, "This is the fourth line\n\n") | |
{ :ok, conn } = chunk(conn, "This is the fifth line\n") | |
conn | |
end | |
end | |
IO.puts "Running MyPlug with Cowboy on http://localhost:4000" | |
Plug.Adapters.Cowboy.http MyPlug, [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
defmodule MyPlug do
import Plug.Connection
def init(options) do
# initialize options
end
def call(conn, _opts) do
conn = send_chunked(conn, 200)
{ :ok, conn } = chunk(conn, "HELLO\n")
:timer.sleep 1000
{ :ok, conn } = chunk(conn, "WORLD\n\n")
:timer.sleep 3000
{ :ok, conn } = chunk(conn, "This is the third line\n")
{ :ok, conn } = chunk(conn, "This is the fourth line\n\n")
{ :ok, conn } = chunk(conn, "This is the fifth line\n")
conn
end
end
IO.puts "Running MyPlug with Cowboy on http://localhost:4000"
Plug.Adapters.Cowboy.http MyPlug, []