From 4e59f4b4e13a4082724ec6b18948c7192d03c94b Mon Sep 17 00:00:00 2001 From: dx3mod Date: Mon, 4 Nov 2024 16:42:24 +0300 Subject: [PATCH] Add natsbyexample directory with fist pub-sub example --- README.md | 37 +++++++++++++-------- examples/natsbyexample/README.md | 4 +++ examples/natsbyexample/dune | 5 +++ examples/natsbyexample/publish_subscribe.ml | 31 +++++++++++++++++ 4 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 examples/natsbyexample/README.md create mode 100644 examples/natsbyexample/dune create mode 100644 examples/natsbyexample/publish_subscribe.ml diff --git a/README.md b/README.md index b15b2b7..d9a07ba 100644 --- a/README.md +++ b/README.md @@ -23,38 +23,47 @@ $ opam pin nats-client-lwt.dev https://github.com/romanchechyotkin/nats.ocaml.gi ### Simple echo example This example shows how to publish to a subject and handle its messages. -Take it from [`examples/simple.ml`](./examples/simple.ml). ```ocaml -let () = - Lwt_main.run @@ +open Lwt.Infix + +let main = (* Create a switch for automatic dispose resources. *) Lwt_switch.with_switch @@ fun switch -> - + (* Connect to a NATS server by address 127.0.0.1:4222 with ECHO flag. *) let%lwt client = Nats_client_lwt.connect ~switch ~settings:[ `Echo ] (Uri.of_string "tcp://127.0.0.1:4222") in - (* Subscribe to HELLO subject. *) - let%lwt hello_subscr = - Nats_client_lwt.sub ~switch client ~subject:"HELLO" () + (* Publish 'hello' message to greet.joe subject. *) + Nats_client_lwt.pub client ~subject:"greet.joe" "hello";%lwt + + (* Subscribe to greet.* subject. *) + let%lwt subscription = + Nats_client_lwt.sub ~switch client ~subject:"greet.*" () in - (* Handle incoming HELLO subject messages. *) - Nats_client_lwt.Subscription.handle hello_subscr (fun msg -> - Lwt_io.printf "HELLO: %s\n" msg.payload.contents); + (* Publishes 'hello' message to three subjects. *) + Lwt_list.iter_p + (fun subject -> Nats_client_lwt.pub client ~subject "hello") + [ "greet.sue"; "greet.bob"; "greet.pam" ];%lwt - (* Send "Hello World" message to HELLO subject. *) - Nats_client_lwt.pub client ~subject:"HELLO" "Hello World";%lwt + (* Handle first three incoming messages to the greet.* subject. *) + Lwt_stream.nget 3 subscription.messages + >>= Lwt_list.iter_s (fun (message : Nats_client.Incoming_message.msg) -> + Lwt_io.printlf "'%s' received on %s" message.payload.contents + message.subject) - Lwt_unix.sleep 0.1 +let () = Lwt_main.run main ``` +Take it from [`examples/natsbyexample/publish_subscribe.ml`](./examples/natsbyexample/publish_subscribe.ml). + ```console $ docker start -a nats-server -$ dune exec ./examples/simple.exe +$ dune exec ./examples/natsbyexample/publish_subscribe.exe ``` ## References diff --git a/examples/natsbyexample/README.md b/examples/natsbyexample/README.md new file mode 100644 index 0000000..5112b79 --- /dev/null +++ b/examples/natsbyexample/README.md @@ -0,0 +1,4 @@ +Adaptation examples from [NATS by example](https://natsbyexample.com/). + +Examples: +- [Core Publish-Subscribe](./publish_subscribe.ml) diff --git a/examples/natsbyexample/dune b/examples/natsbyexample/dune new file mode 100644 index 0000000..b5d8e78 --- /dev/null +++ b/examples/natsbyexample/dune @@ -0,0 +1,5 @@ +(executables + (names publish_subscribe) + (libraries nats-client-lwt lwt) + (preprocess + (pps lwt_ppx))) diff --git a/examples/natsbyexample/publish_subscribe.ml b/examples/natsbyexample/publish_subscribe.ml new file mode 100644 index 0000000..56b956a --- /dev/null +++ b/examples/natsbyexample/publish_subscribe.ml @@ -0,0 +1,31 @@ +open Lwt.Infix + +let main = + (* Create a switch for automatic dispose resources. *) + Lwt_switch.with_switch @@ fun switch -> + (* Connect to a NATS server by address 127.0.0.1:4222 with ECHO flag. *) + let%lwt client = + Nats_client_lwt.connect ~switch ~settings:[ `Echo ] + (Uri.of_string "tcp://127.0.0.1:4222") + in + + (* Publish 'hello' message to greet.joe subject. *) + Nats_client_lwt.pub client ~subject:"greet.joe" "hello";%lwt + + (* Subscribe to greet.* subject. *) + let%lwt subscription = + Nats_client_lwt.sub ~switch client ~subject:"greet.*" () + in + + (* Publishes 'hello' message to three subjects. *) + Lwt_list.iter_p + (fun subject -> Nats_client_lwt.pub client ~subject "hello") + [ "greet.sue"; "greet.bob"; "greet.pam" ];%lwt + + (* Handle first three incoming messages to the greet.* subject. *) + Lwt_stream.nget 3 subscription.messages + >>= Lwt_list.iter_s (fun (message : Nats_client.Incoming_message.msg) -> + Lwt_io.printlf "'%s' received on %s" message.payload.contents + message.subject) + +let () = Lwt_main.run main