Connection being dropped before chunk finishes

Hello everyone I’m trying to read a file.json and return chunks to the client. Everything works great with the current implementation but when I try to simulate “heavy work” by adding a sleep between chunks the connections ends before all the file has been read. Anyone knows what it might be? Here’s the current code.

get "/" do

    conn = conn
    |> put_resp_content_type("text/event-stream")
    |> send_chunked(200)

    File.stream!("pokemon.json")
    |> Jaxon.Stream.from_enumerable
    |> Jaxon.Stream.query([:root, :all])
    |> Stream.each(fn element ->
     {:ok, pokemon}  = Jason.encode(element)

     IO.puts "id: #{element["id"]}"
      conn |> chunk(pokemon)
     Process.sleep(200)
    end )
    |> Stream.run

    conn
  end

EDIT

Was able to identify the problem. Needed to increase idle_timeout on plug_cowboy

example:

 {Plug.Cowboy, scheme: :http, plug: StreamExample.Router, options: [port: 8080, protocol_options: [
        idle_timeout: :infinity # => fixes (on production should not be set to infinity)
      ]]}
2 Likes