2021-04-07 16:58:43 +03:00
..
2021-03-28 21:48:59 +03:00
2021-03-27 21:08:27 +03:00
2021-04-07 16:58:43 +03:00
2021-04-07 16:58:43 +03:00

j-stream


In example 6-echo, we echoed POST requests by reading their whole bodies into memory, and then writing them. Here, we echo request bodies chunk by chunk:

let echo request response =
  let rec loop () =
    match%lwt Dream.read request with
    | None ->
      Dream.close_stream response
    | Some chunk ->
      let%lwt () = Dream.write chunk response in
      let%lwt () = Dream.flush response in
      loop ()
  in
  loop ()

let () =
  Dream.run
  @@ Dream.logger
  @@ Dream.router [
    Dream.post "/echo" (fun request ->
      Dream.stream (echo request));
  ]
  @@ Dream.not_found
$ dune exec --root . ./stream.exe

You can test it by running

curl -X POST http://localhost:8080/echo -T -

You will see the server responding immediately to each line on STDIN.

See Streaming in the API docs.


Next steps:

  • k-websocket sends and receives messages over a WebSocket.
  • l-https enables HTTPS, which is very easy with Dream.

Up to the tutorial index