Anton Bachin 20f62d209e Convert most examples to opam
Reason examples still use esy.
2023-11-29 18:18:33 +03:00

1.3 KiB

w-template-stream


This example streams a template as a response body. It sends one paragraph per second to the client:

let render response =
  %% response
  <html>
  <body>

%   let rec paragraphs index =
      <p><%i index %></p>
%     let%lwt () = Dream.flush response in
%     let%lwt () = Lwt_unix.sleep 1. in
%     paragraphs (index + 1)
%   in
%   let%lwt () = paragraphs 0 in

  </body>
  </html>

let () =
  Dream.run
  @@ Dream.logger
  @@ fun _ -> Dream.stream ~headers:["Content-Type", Dream.text_html] render
$ cd example/w-template-stream
$ opam install --deps-only --yes .
$ dune exec --root . ./template_stream.exe

Try it in the playground.


Most uses of streaming don't need Dream.flush, but we are using it here to prevent buffering of each paragraph.


See also:

  • 7-template section Security on security considerations with templates, and in general.
  • r-template-stream is a Reason syntax version of this example.

Up to the example index