mirror of
https://github.com/aantron/dream.git
synced 2025-10-02 00:03:57 -04:00
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.