Skip to content

Commit e39c8f2

Browse files
committed
update readme
1 parent 0f5ef66 commit e39c8f2

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,27 @@ let print_param =
8282
put "/hello/:name" (fun req ->
8383
`String ("Hello " ^ param req "name") |> respond')
8484
85+
let streaming =
86+
let open Lwt.Infix in
87+
get "/hello/stream" (fun _req ->
88+
(* [create_stream] returns a push function that can be used to
89+
push new content onto the stream. [f] is function that
90+
expects to receive a promise that gets resolved when the user
91+
decides that they have pushed all their content onto the stream.
92+
When the promise forwarded to [f] gets resolved, the stream will be
93+
closed. *)
94+
let f, push = App.create_stream () in
95+
let timers =
96+
List.map
97+
(fun t ->
98+
Lwt_unix.sleep t
99+
>|= fun () -> push (Printf.sprintf "Hello after %f seconds\n" t))
100+
[1.; 2.; 3.]
101+
in
102+
f (Lwt.join timers))
103+
85104
let default =
86-
not_found (fun req ->
105+
not_found (fun _req ->
87106
`Json Ezjsonm.(dict [("message", string "Route not found")]) |> respond')
88107
89108
let print_person =
@@ -93,7 +112,9 @@ let print_person =
93112
in
94113
`Json (person |> json_of_person) |> respond')
95114
96-
let _ = App.empty |> print_param |> print_person |> default |> App.run_command
115+
let _ =
116+
App.empty |> print_param |> print_person |> streaming |> default
117+
|> App.run_command
97118
```
98119

99120
compile and run with:

examples/hello_world.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ let print_param =
1313
let streaming =
1414
let open Lwt.Infix in
1515
get "/hello/stream" (fun _req ->
16+
(* [create_stream] returns a push function that can be used to
17+
push new content onto the stream. [f] is function that
18+
expects to receive a promise that gets resolved when the user
19+
decides that they have pushed all their content onto the stream.
20+
When the promise forwarded to [f] gets resolved, the stream will be
21+
closed. *)
1622
let f, push = App.create_stream () in
1723
let timers =
1824
List.map

0 commit comments

Comments
 (0)