nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
}
defer nc.Close()
// Send the request
msg, err := nc.Request("time", nil, time.Second)
if err != nil {
log.Fatal(err)
}
// Use the response
log.Printf("Reply: %s", msg.Data)
// Close the connection
nc.Close()
Connection nc = Nats.connect("nats://demo.nats.io:4222");
// Send the request
Message msg = nc.request("time", null, Duration.ofSeconds(1));
// Use the response
System.out.println(new String(msg.getData(), StandardCharsets.UTF_8));
// Close the connection
nc.close();
let nc = NATS.connect({url: "nats://demo.nats.io:4222"});
// set up a subscription to process the request
nc.subscribe('time', (msg, reply) => {
if(reply) {
nc.publish(reply, new Date().toLocaleTimeString());
}
});
nc.requestOne('time', (msg) => {
t.log('the time is', msg);
nc.close();
});
nc = NATS()
async def sub(msg):
await nc.publish(msg.reply, b'response')
await nc.connect(servers=["nats://demo.nats.io:4222"])
await nc.subscribe("time", cb=sub)
# Send the request
try:
msg = await nc.request("time", b'', timeout=1)
# Use the response
print("Reply:", msg)
except asyncio.TimeoutError:
print("Timed out waiting for response")
require 'nats/client'
require 'fiber'
NATS.start(servers:["nats://127.0.0.1:4222"]) do |nc|
nc.subscribe("time") do |msg, reply|
nc.publish(reply, "response")
end
Fiber.new do
# Use the response
msg = nc.request("time", "")
puts "Reply: #{msg}"
end.resume
end
let msg = await nc.request('time', 1000);
t.log('the time is', msg.data);
nc.close();