Skip to content

Commit

Permalink
Add natsbyexample directory with fist pub-sub example
Browse files Browse the repository at this point in the history
  • Loading branch information
dx3mod committed Nov 4, 2024
1 parent c43faec commit 4e59f4b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
37 changes: 23 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions examples/natsbyexample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Adaptation examples from [NATS by example](https://natsbyexample.com/).

Examples:
- [Core Publish-Subscribe](./publish_subscribe.ml)
5 changes: 5 additions & 0 deletions examples/natsbyexample/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(executables
(names publish_subscribe)
(libraries nats-client-lwt lwt)
(preprocess
(pps lwt_ppx)))
31 changes: 31 additions & 0 deletions examples/natsbyexample/publish_subscribe.ml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4e59f4b

Please sign in to comment.