mirror of
https://github.com/aantron/dream.git
synced 2025-12-15 00:05:29 -05: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 =
let%lwt () =
%% 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>
in
Dream.close_stream response
let () =
Dream.run
@@ Dream.logger
@@ fun _ -> Dream.stream render
$ npm install esy && npx esy
$ npx esy start
Most uses of streaming don't need
Dream.flush, but we are using it
here to prevent buffering of each paragraph.
See also:
7-templatesection Security on security considerations with templates, and in general.r-template-streamis a Reason syntax version of this example.