mirror of
https://github.com/taigrr/nats.docs
synced 2025-01-18 04:03:23 -08:00
187 lines
6.9 KiB
HTML
187 lines
6.9 KiB
HTML
|
|
<div class="tab-wrap">
|
|
|
|
|
|
<input type="radio" id="publish_with_reply_go" name="publish_with_reply" class="tab" checked>
|
|
|
|
<label for="publish_with_reply_go" class="api-lang" data-language="go">Go</label>
|
|
|
|
|
|
<input type="radio" id="publish_with_reply_java" name="publish_with_reply" class="tab">
|
|
|
|
<label for="publish_with_reply_java" class="api-lang" data-language="java">Java</label>
|
|
|
|
|
|
<input type="radio" id="publish_with_reply_js" name="publish_with_reply" class="tab">
|
|
|
|
<label for="publish_with_reply_js" class="api-lang" data-language="js">JavaScript</label>
|
|
|
|
|
|
<input type="radio" id="publish_with_reply_py" name="publish_with_reply" class="tab">
|
|
|
|
<label for="publish_with_reply_py" class="api-lang" data-language="py">Python</label>
|
|
|
|
|
|
<input type="radio" id="publish_with_reply_ruby" name="publish_with_reply" class="tab">
|
|
|
|
<label for="publish_with_reply_ruby" class="api-lang" data-language="ruby">Ruby</label>
|
|
|
|
|
|
<input type="radio" id="publish_with_reply_ts" name="publish_with_reply" class="tab">
|
|
|
|
<label for="publish_with_reply_ts" class="api-lang" data-language="ts">TypeScript</label>
|
|
|
|
|
|
|
|
<div class="tab__content">
|
|
<pre id="publish_with_reply_go_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/go-nats-examples/blob/master/api-examples/publish_with_reply/main.go#L11-43"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-go">nc, err := nats.Connect("demo.nats.io")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer nc.Close()
|
|
|
|
// Create a unique subject name
|
|
uniqueReplyTo := nats.NewInbox()
|
|
|
|
// Listen for a single response
|
|
sub, err := nc.SubscribeSync(uniqueReplyTo)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Send the request
|
|
if err := nc.PublishRequest("time", uniqueReplyTo, nil); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Read the reply
|
|
msg, err := sub.NextMsg(time.Second)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Use the response
|
|
log.Printf("Reply: %s", msg.Data)
|
|
|
|
// Close the connection
|
|
nc.Close()
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="publish_with_reply_java_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/java-nats-examples/blob/master/src/main/java/io/nats/examples/PublishWithReply.java#L16-37"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-java">Connection nc = Nats.connect("nats://demo.nats.io:4222");
|
|
|
|
// Create a unique subject name
|
|
String uniqueReplyTo = NUID.nextGlobal();
|
|
|
|
// Listen for a single response
|
|
Subscription sub = nc.subscribe(uniqueReplyTo);
|
|
sub.unsubscribe(1);
|
|
|
|
// Send the request
|
|
nc.publish("time", uniqueReplyTo, null);
|
|
|
|
// Read the reply
|
|
Message msg = sub.nextMessage(Duration.ofSeconds(1));
|
|
|
|
// Use the response
|
|
System.out.println(new String(msg.getData(), StandardCharsets.UTF_8));
|
|
|
|
// Close the connection
|
|
nc.close();
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="publish_with_reply_js_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/node-nats-examples/blob/master/src/publisher_samples.js#L41-57"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-javascript">// set up a subscription to process the request
|
|
nc.subscribe('time', (msg, reply) => {
|
|
if(reply) {
|
|
nc.publish(reply, new Date().toLocaleTimeString());
|
|
}
|
|
});
|
|
|
|
// create a subscription subject that the responding send replies to
|
|
let inbox = NATS.createInbox();
|
|
nc.subscribe(inbox, {max: 1}, (msg) => {
|
|
t.log('the time is', msg);
|
|
nc.close();
|
|
});
|
|
|
|
nc.publish('time', "", inbox);
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="publish_with_reply_py_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/asyncio-nats-examples/blob/master/publish_with_reply.py#L8-27"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-python">nc = NATS()
|
|
|
|
future = asyncio.Future()
|
|
|
|
async def sub(msg):
|
|
nonlocal future
|
|
future.set_result(msg)
|
|
|
|
await nc.connect(servers=["nats://demo.nats.io:4222"])
|
|
await nc.subscribe("time", cb=sub)
|
|
|
|
unique_reply_to = new_inbox()
|
|
await nc.publish_request("time", unique_reply_to, b'')
|
|
|
|
# Use the response
|
|
msg = await asyncio.wait_for(future, 1)
|
|
print("Reply:", msg)
|
|
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="publish_with_reply_ruby_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ruby-nats-examples/blob/master/publish_with_reply.rb#L1-22"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-ruby">require 'nats/client'
|
|
require 'fiber'
|
|
|
|
NATS.start(servers:["nats://127.0.0.1:4222"]) do |nc|
|
|
Fiber.new do
|
|
f = Fiber.current
|
|
|
|
nc.subscribe("time") do |msg, reply|
|
|
f.resume msg
|
|
end
|
|
|
|
nc.publish("time", 'example', NATS.create_inbox)
|
|
|
|
# Use the response
|
|
msg = Fiber.yield
|
|
puts "Reply: #{msg}"
|
|
|
|
end.resume
|
|
end
|
|
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="publish_with_reply_ts_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ts-nats-examples/blob/master/src/publisher_samples.ts#L41-64"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-javascript">// set up a subscription to process the request
|
|
await nc.subscribe('time', (err, msg) => {
|
|
if (err) {
|
|
// this example is running inside of a promise
|
|
reject();
|
|
return;
|
|
}
|
|
if (msg.reply) {
|
|
nc.publish(msg.reply, new Date().toLocaleTimeString());
|
|
}
|
|
});
|
|
|
|
// create a subscription subject that the responding send replies to
|
|
let inbox = createInbox();
|
|
await nc.subscribe(inbox, (err, msg) => {
|
|
t.log('the time is', msg.data);
|
|
// this example is running inside of a promise
|
|
nc.close();
|
|
resolve();
|
|
}, {max: 1});
|
|
|
|
nc.publish('time', "", inbox);
|
|
</code></pre>
|
|
</div>
|
|
|
|
</div>
|