mirror of
https://github.com/taigrr/nats.docs
synced 2025-01-18 04:03:23 -08:00
163 lines
6.3 KiB
HTML
163 lines
6.3 KiB
HTML
|
|
<div class="tab-wrap">
|
|
|
|
|
|
<input type="radio" id="subscribe_w_reply_go" name="subscribe_w_reply" class="tab" checked>
|
|
|
|
<label for="subscribe_w_reply_go" class="api-lang" data-language="go">Go</label>
|
|
|
|
|
|
<input type="radio" id="subscribe_w_reply_java" name="subscribe_w_reply" class="tab">
|
|
|
|
<label for="subscribe_w_reply_java" class="api-lang" data-language="java">Java</label>
|
|
|
|
|
|
<input type="radio" id="subscribe_w_reply_js" name="subscribe_w_reply" class="tab">
|
|
|
|
<label for="subscribe_w_reply_js" class="api-lang" data-language="js">JavaScript</label>
|
|
|
|
|
|
<input type="radio" id="subscribe_w_reply_py" name="subscribe_w_reply" class="tab">
|
|
|
|
<label for="subscribe_w_reply_py" class="api-lang" data-language="py">Python</label>
|
|
|
|
|
|
<input type="radio" id="subscribe_w_reply_ruby" name="subscribe_w_reply" class="tab">
|
|
|
|
<label for="subscribe_w_reply_ruby" class="api-lang" data-language="ruby">Ruby</label>
|
|
|
|
|
|
<input type="radio" id="subscribe_w_reply_ts" name="subscribe_w_reply" class="tab">
|
|
|
|
<label for="subscribe_w_reply_ts" class="api-lang" data-language="ts">TypeScript</label>
|
|
|
|
|
|
|
|
<div class="tab__content">
|
|
<pre id="subscribe_w_reply_go_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/go-nats-examples/blob/master/api-examples/subscribe_w_reply/main.go#L11-39"><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()
|
|
|
|
// Subscribe
|
|
sub, err := nc.SubscribeSync("time")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Read a message
|
|
msg, err := sub.NextMsg(10 * time.Second)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Get the time
|
|
timeAsBytes := []byte(time.Now().String())
|
|
|
|
// Send the time
|
|
nc.Publish(msg.Reply, timeAsBytes)
|
|
|
|
// Flush and close the connection
|
|
nc.Flush()
|
|
nc.Close()
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="subscribe_w_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/SubscribeWithReply.java#L17-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");
|
|
|
|
// Subscribe
|
|
Subscription sub = nc.subscribe("time");
|
|
|
|
// Read a message
|
|
Message msg = sub.nextMessage(Duration.ZERO);
|
|
|
|
// Get the time
|
|
Calendar cal = Calendar.getInstance();
|
|
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
|
|
byte[] timeAsBytes = sdf.format(cal.getTime()).getBytes(StandardCharsets.UTF_8);
|
|
|
|
// Send the time
|
|
nc.publish(msg.getReplyTo(), timeAsBytes);
|
|
|
|
// Flush and close the connection
|
|
nc.flush(Duration.ZERO);
|
|
nc.close();
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="subscribe_w_reply_js_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/node-nats-examples/blob/master/src/subscriber_samples.js#L28-35"><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 a request
|
|
nc.subscribe('time', (msg, reply) => {
|
|
if (msg.reply) {
|
|
nc.publish(msg.reply, new Date().toLocaleTimeString());
|
|
}
|
|
});
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="subscribe_w_reply_py_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/asyncio-nats-examples/blob/master/subscribe_w_reply.py#L8-31"><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()
|
|
|
|
await nc.connect(servers=["nats://demo.nats.io:4222"])
|
|
|
|
future = asyncio.Future()
|
|
|
|
async def cb(msg):
|
|
nonlocal future
|
|
future.set_result(msg)
|
|
|
|
await nc.subscribe("time", cb=cb)
|
|
|
|
await nc.publish_request("time", new_inbox(), b'What is the time?')
|
|
await nc.flush()
|
|
|
|
# Read the message
|
|
msg = await asyncio.wait_for(future, 1)
|
|
|
|
# Send the time
|
|
time_as_bytes = "{}".format(datetime.now()).encode()
|
|
await nc.publish(msg.reply, time_as_bytes)
|
|
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="subscribe_w_reply_ruby_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ruby-nats-examples/blob/master/subscribe_w_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 Time.now
|
|
end
|
|
|
|
nc.publish("time", 'What is the time?', NATS.create_inbox)
|
|
|
|
# Use the response
|
|
msg = Fiber.yield
|
|
puts "Reply: #{msg}"
|
|
|
|
end.resume
|
|
end
|
|
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="subscribe_w_reply_ts_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ts-nats-examples/blob/master/src/subscriber_samples.ts#L29-38"><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 a request
|
|
await nc.subscribe('time', (err, msg) => {
|
|
if (msg.reply) {
|
|
nc.publish(msg.reply, new Date().toLocaleTimeString());
|
|
} else {
|
|
t.log('got a request for the time, but no reply subject was set.');
|
|
}
|
|
});
|
|
</code></pre>
|
|
</div>
|
|
|
|
</div>
|