-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add natsbyexample directory with fist pub-sub example
- Loading branch information
Showing
4 changed files
with
63 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |