Is it possible to keep connection open for few seconds in chunked state?
In request I create few processes which takes lets say 5 seconds to finish and after that I want to send response via chunk to client, but connection is closed
I am testing this with postman and I have this headers:
"Connection": "Keep-Alive", "Keep-Alive": "timeout=10000"
This is my minimized application, output is always "InitEnd" desired result is: "Init1234567789End" or something like this:
defmodule AlivePlug do
import Plug.Conn
def init(opts) do
opts
end
def call(conn, _opts) do
conn = send_chunked(conn, 200)
# send initial chunk
chunk(conn, "Init")
pid = start_thread
# create 10 async processes which does somthing for 5 seconds and then sends result via chunk
1..10
|> Enum.each(fn num ->
send pid, {conn, num}
end)
# send End as a symbol of last chunk
chunk(conn, "End")
conn
end
defp start_thread, do: spawn_link(fn -> thread_listener end)
defp thread_listener do
receive do
{conn, num} ->
:timer.sleep(5000)
# problem is here :(
# chunk returns {:error, :closed}
{:error, :closed} = chunk(conn, "#{num}")
thread_listener
_ ->
thread_listener
end
end
end