mirror of
				https://github.com/taigrr/nats.docs
				synced 2025-01-18 04:03:23 -08:00 
			
		
		
		
	
		
			
				
	
	
		
			135 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			135 lines
		
	
	
		
			5.6 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("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()
 | |
| </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("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();
 | |
| </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#L66-80"><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 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();
 | |
| });
 | |
| </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'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")
 | |
| 
 | |
| </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 '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
 | |
| 
 | |
| </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#L78-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">let msg = await nc.request('time', 1000);
 | |
| t.log('the time is', msg.data);
 | |
| nc.close();
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| </div>
 |