Skip to content

Instantly share code, notes, and snippets.

@eezis
Created March 23, 2014 19:09
Show Gist options
  • Save eezis/9728126 to your computer and use it in GitHub Desktop.
Save eezis/9728126 to your computer and use it in GitHub Desktop.
Simple Streaming with Elixir Plug.
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, []
@eezis
Copy link
Author

eezis commented Mar 23, 2014

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")
: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, []

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment