Skip to content

Commit

Permalink
Add automatic inbox for requests
Browse files Browse the repository at this point in the history
  • Loading branch information
dx3mod committed Nov 24, 2024
1 parent 909df02 commit 7ed729f
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lwt/nats_client_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ let unsub client ?max_msgs sid =
Connection.Send.unsub client.connection ?max_msgs sid

(** [sub client ~subject ?sid ()] subscribe on the subject and get stream. *)
let sub ?switch client ~subject ?(sid : Sid.t option) () =
let sub ?switch client ~subject ?sid () =
let sid = Option.value ~default:(Sid.create 9) sid in
Connection.Send.sub ~subject ~sid ~queue_group:None client.connection;%lwt

Expand All @@ -88,13 +88,13 @@ let sub ?switch client ~subject ?(sid : Sid.t option) () =
(* TODO: make drain method, unsub all subscribers *)

let request client ~subject payload =
(* Inbox for replies. *)
let inbox = Printf.sprintf "_INBOX.%s" @@ Nats_client.Sid.create 9 in

Lwt_switch.with_switch @@ fun switch ->
let%lwt subscription =
(* TODO: unique subject for subscription *)
sub ~switch client ~subject:"for-request-mechanism" ()
in
let%lwt subscription = sub ~switch client ~subject:inbox () in

pub client ~subject ~reply_to:"for-request-mechanism" payload;%lwt
pub client ~subject ~reply_to:inbox payload;%lwt

let%lwt incoming_message = Lwt_stream.next subscription.messages in
Lwt.return incoming_message.payload

0 comments on commit 7ed729f

Please sign in to comment.