Powered By GitBook
Including a Reply Subject
The optional reply-to field when publishing a message can be used on the receiving side to respond. The reply-to subject is often called an inbox, and most libraries may provide a method for generating unique inbox subjects. Most libraries also provide for the request-reply pattern with a single call. For example to send a request to the subject time, with no content for the messages, you might:
Go
Java
JavaScript
Python
Ruby
TypeScript
C
1
nc, err := nats.Connect("demo.nats.io")
2
if err != nil {
3
log.Fatal(err)
4
}
5
defer nc.Close()
6
7
// Create a unique subject name for replies.
8
uniqueReplyTo := nats.NewInbox()
9
10
// Listen for a single response
11
sub, err := nc.SubscribeSync(uniqueReplyTo)
12
if err != nil {
13
log.Fatal(err)
14
}
15
16
// Send the request.
17
// If processing is synchronous, use Request() which returns the response message.
18
if err := nc.PublishRequest("time", uniqueReplyTo, nil); err != nil {
19
log.Fatal(err)
20
}
21
22
// Read the reply
23
msg, err := sub.NextMsg(time.Second)
24
if err != nil {
25
log.Fatal(err)
26
}
27
28
// Use the response
29
log.Printf("Reply: %s", msg.Data)
Copied!
1
Connection nc = Nats.connect("nats://demo.nats.io:4222");
2
3
// Create a unique subject name
4
String uniqueReplyTo = NUID.nextGlobal();
5
6
// Listen for a single response
7
Subscription sub = nc.subscribe(uniqueReplyTo);
8
sub.unsubscribe(1);
9
10
// Send the request
11
nc.publish("time", uniqueReplyTo, null);
12
13
// Read the reply
14
Message msg = sub.nextMessage(Duration.ofSeconds(1));
15
16
// Use the response
17
System.out.println(new String(msg.getData(), StandardCharsets.UTF_8));
18
19
// Close the connection
20
nc.close();
Copied!
1
let nc = NATS.connect({url: "nats://demo.nats.io:4222"});
2
// set up a subscription to process the request
3
nc.subscribe('time', (msg, reply) => {
4
if(reply) {
5
nc.publish(reply, new Date().toLocaleTimeString());
6
}
7
});
8
9
// create a subscription subject that the responding send replies to
10
let inbox = NATS.createInbox();
11
nc.subscribe(inbox, {max: 1}, (msg) => {
12
t.log('the time is', msg);
13
nc.close();
14
});
15
16
nc.publish('time', "", inbox);
Copied!
1
nc = NATS()
2
3
future = asyncio.Future()
4
5
async def sub(msg):
6
nonlocal future
7
future.set_result(msg)
8
9
await nc.connect(servers=["nats://demo.nats.io:4222"])
10
await nc.subscribe("time", cb=sub)
11
12
unique_reply_to = new_inbox()
13
await nc.publish_request("time", unique_reply_to, b'')
14
15
# Use the response
16
msg = await asyncio.wait_for(future, 1)
17
print("Reply:", msg)
Copied!
1
require 'nats/client'
2
require 'fiber'
3
4
NATS.start(servers:["nats://127.0.0.1:4222"]) do |nc|
5
Fiber.new do
6
f = Fiber.current
7
8
nc.subscribe("time") do |msg, reply|
9
f.resume msg
10
end
11
12
nc.publish("time", 'example', NATS.create_inbox)
13
14
# Use the response
15
msg = Fiber.yield
16
puts "Reply: #{msg}"
17
18
end.resume
19
end
Copied!
1
// set up a subscription to process the request
2
await nc.subscribe('time', (err, msg) => {
3
if (err) {
4
// this example is running inside of a promise
5
reject();
6
return;
7
}
8
if (msg.reply) {
9
nc.publish(msg.reply, new Date().toLocaleTimeString());
10
}
11
});
12
13
// create a subscription subject that the responding send replies to
14
let inbox = createInbox();
15
await nc.subscribe(inbox, (err, msg) => {
16
t.log('the time is', msg.data);
17
// this example is running inside of a promise
18
nc.close();
19
resolve();
20
}, {max: 1});
21
22
nc.publish('time', "", inbox);
Copied!
1
natsConnection *conn = NULL;
2
natsStatus s = NATS_OK;
3
4
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
5
// Publish a message and provide a reply subject
6
if (s == NATS_OK)
7
s = natsConnection_PublishRequestString(conn, "request", "reply", "this is the request");
8
9
(...)
10
11
// Destroy objects that were created
12
natsConnection_Destroy(conn);
Copied!
Last modified 1yr ago
Export as PDF
Copy link