1
0
mirror of https://github.com/taigrr/nats.docs synced 2025-01-18 04:03:23 -08:00
nats.docs/_examples/request_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

126 lines
5.4 KiB
HTML

<div class="tab-wrap">
<input type="radio" id="request_reply_go" name="request_reply" class="tab" checked>
<label for="request_reply_go" class="api-lang" data-language="go">Go</label>
<input type="radio" id="request_reply_java" name="request_reply" class="tab">
<label for="request_reply_java" class="api-lang" data-language="java">Java</label>
<input type="radio" id="request_reply_js" name="request_reply" class="tab">
<label for="request_reply_js" class="api-lang" data-language="js">JavaScript</label>
<input type="radio" id="request_reply_py" name="request_reply" class="tab">
<label for="request_reply_py" class="api-lang" data-language="py">Python</label>
<input type="radio" id="request_reply_ruby" name="request_reply" class="tab">
<label for="request_reply_ruby" class="api-lang" data-language="ruby">Ruby</label>
<input type="radio" id="request_reply_ts" name="request_reply" class="tab">
<label for="request_reply_ts" class="api-lang" data-language="ts">TypeScript</label>
<div class="tab__content">
<pre id="request_reply_go_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/go-nats-examples/blob/master/api-examples/request_reply/main.go#L11-29"><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()
// Send the request
msg, err := nc.Request(&#34;time&#34;, nil, time.Second)
if err != nil {
log.Fatal(err)
}
// Use the response
log.Printf(&#34;Reply: %s&#34;, msg.Data)
// Close the connection
nc.Close()
</code></pre>
</div>
<div class="tab__content">
<pre id="request_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/RequestReply.java#L14-25"><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;);
// Send the request
Message msg = nc.request(&#34;time&#34;, null, 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="request_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#L77-82"><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">nc.requestOne(&#39;time&#39;, (msg) =&gt; {
t.log(&#39;the time is&#39;, msg);
nc.close();
});
</code></pre>
</div>
<div class="tab__content">
<pre id="request_reply_py_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/asyncio-nats-examples/blob/master/request_reply.py#L8-25"><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()
async def sub(msg):
await nc.publish(msg.reply, b&#39;response&#39;)
await nc.connect(servers=[&#34;nats://demo.nats.io:4222&#34;])
await nc.subscribe(&#34;time&#34;, cb=sub)
# Send the request
try:
msg = await nc.request(&#34;time&#34;, b&#39;&#39;, timeout=1)
# Use the response
print(&#34;Reply:&#34;, msg)
except asyncio.TimeoutError:
print(&#34;Timed out waiting for response&#34;)
</code></pre>
</div>
<div class="tab__content">
<pre id="request_reply_ruby_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ruby-nats-examples/blob/master/request_reply.rb#L1-17"><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|
nc.subscribe(&#34;time&#34;) do |msg, reply|
nc.publish(reply, &#34;response&#34;)
end
Fiber.new do
# Use the response
msg = nc.request(&#34;time&#34;, &#34;&#34;)
puts &#34;Reply: #{msg}&#34;
end.resume
end
</code></pre>
</div>
<div class="tab__content">
<pre id="request_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#L81-85"><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">let msg = await nc.request(&#39;time&#39;, 1000);
t.log(&#39;the time is&#39;, msg.data);
nc.close();
</code></pre>
</div>
</div>