1
0
mirror of https://github.com/taigrr/nats.docs synced 2025-01-18 04:03:23 -08:00
nats.docs/_examples/subscribe_w_reply.html
Stephen Asbury 57db99feba Cleaned up how examples are handled to reduce thrashing in git
Examples are no longer in the final html as solo files, only in their container file
2019-05-15 10:41:36 -07:00

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(&#34;demo.nats.io&#34;)
if err != nil {
log.Fatal(err)
}
defer nc.Close()
// Subscribe
sub, err := nc.SubscribeSync(&#34;time&#34;)
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(&#34;nats://demo.nats.io:4222&#34;);
// Subscribe
Subscription sub = nc.subscribe(&#34;time&#34;);
// Read a message
Message msg = sub.nextMessage(Duration.ZERO);
// Get the time
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(&#34;HH:mm:ss&#34;);
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(&#39;time&#39;, (msg, reply) =&gt; {
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=[&#34;nats://demo.nats.io:4222&#34;])
future = asyncio.Future()
async def cb(msg):
nonlocal future
future.set_result(msg)
await nc.subscribe(&#34;time&#34;, cb=cb)
await nc.publish_request(&#34;time&#34;, new_inbox(), b&#39;What is the time?&#39;)
await nc.flush()
# Read the message
msg = await asyncio.wait_for(future, 1)
# Send the time
time_as_bytes = &#34;{}&#34;.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 &#39;nats/client&#39;
require &#39;fiber&#39;
NATS.start(servers:[&#34;nats://127.0.0.1:4222&#34;]) do |nc|
Fiber.new do
f = Fiber.current
nc.subscribe(&#34;time&#34;) do |msg, reply|
f.resume Time.now
end
nc.publish(&#34;time&#34;, &#39;What is the time?&#39;, NATS.create_inbox)
# Use the response
msg = Fiber.yield
puts &#34;Reply: #{msg}&#34;
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(&#39;time&#39;, (err, msg) =&gt; {
if (msg.reply) {
nc.publish(msg.reply, new Date().toLocaleTimeString());
} else {
t.log(&#39;got a request for the time, but no reply subject was set.&#39;);
}
});
</code></pre>
</div>
</div>