mirror of
				https://github.com/taigrr/nats.docs
				synced 2025-01-18 04:03:23 -08:00 
			
		
		
		
	
		
			
				
	
	
		
			136 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			136 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
 | 
						|
<div class="tab-wrap">
 | 
						|
  	
 | 
						|
	
 | 
						|
	<input type="radio" id="flush_go" name="flush" class="tab" checked>
 | 
						|
	
 | 
						|
	<label for="flush_go" class="api-lang" data-language="go">Go</label>
 | 
						|
	
 | 
						|
	
 | 
						|
	<input type="radio" id="flush_java" name="flush" class="tab">
 | 
						|
	
 | 
						|
	<label for="flush_java" class="api-lang" data-language="java">Java</label>
 | 
						|
	
 | 
						|
	
 | 
						|
	<input type="radio" id="flush_js" name="flush" class="tab">
 | 
						|
	
 | 
						|
	<label for="flush_js" class="api-lang" data-language="js">JavaScript</label>
 | 
						|
	
 | 
						|
	
 | 
						|
	<input type="radio" id="flush_py" name="flush" class="tab">
 | 
						|
	
 | 
						|
	<label for="flush_py" class="api-lang" data-language="py">Python</label>
 | 
						|
	
 | 
						|
	
 | 
						|
	<input type="radio" id="flush_ruby" name="flush" class="tab">
 | 
						|
	
 | 
						|
	<label for="flush_ruby" class="api-lang" data-language="ruby">Ruby</label>
 | 
						|
	
 | 
						|
	
 | 
						|
	<input type="radio" id="flush_ts" name="flush" class="tab">
 | 
						|
	
 | 
						|
	<label for="flush_ts" class="api-lang" data-language="ts">TypeScript</label>
 | 
						|
	
 | 
						|
 | 
						|
  	
 | 
						|
	<div class="tab__content">
 | 
						|
	<pre id="flush_go_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/go-nats-examples/blob/master/api-examples/flush/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()
 | 
						|
 | 
						|
// Just to not collide using the demo server with other users.
 | 
						|
subject := nats.NewInbox()
 | 
						|
 | 
						|
if err := nc.Publish(subject, []byte("All is Well")); err != nil {
 | 
						|
	log.Fatal(err)
 | 
						|
}
 | 
						|
// Sends a PING and wait for a PONG from the server, up to the given timeout.
 | 
						|
// This gives guarantee that the server has processed the above message.
 | 
						|
if err := nc.FlushTimeout(time.Second); err != nil {
 | 
						|
	log.Fatal(err)
 | 
						|
}
 | 
						|
</code></pre>
 | 
						|
	</div>
 | 
						|
	
 | 
						|
	<div class="tab__content">
 | 
						|
	<pre id="flush_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/Flush.java#L13-20"><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");
 | 
						|
 | 
						|
nc.publish("updates", "All is Well".getBytes(StandardCharsets.UTF_8));
 | 
						|
nc.flush(Duration.ofSeconds(1)); // Flush the message queue
 | 
						|
 | 
						|
nc.close();
 | 
						|
</code></pre>
 | 
						|
	</div>
 | 
						|
	
 | 
						|
	<div class="tab__content">
 | 
						|
	<pre id="flush_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#L91-101"><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"});
 | 
						|
let start = Date.now();
 | 
						|
nc.flush(() => {
 | 
						|
    t.log('round trip completed in', Date.now() - start, 'ms');
 | 
						|
});
 | 
						|
 | 
						|
nc.publish('foo');
 | 
						|
// function in flush is optional
 | 
						|
nc.flush();
 | 
						|
</code></pre>
 | 
						|
	</div>
 | 
						|
	
 | 
						|
	<div class="tab__content">
 | 
						|
	<pre id="flush_py_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/asyncio-nats-examples/blob/master/flush.py#L6-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-python">nc = NATS()
 | 
						|
 | 
						|
await nc.connect(servers=["nats://demo.nats.io:4222"])
 | 
						|
 | 
						|
await nc.publish("updates", b'All is Well')
 | 
						|
 | 
						|
# Sends a PING and wait for a PONG from the server, up to the given timeout.
 | 
						|
# This gives guarantee that the server has processed above message.
 | 
						|
await nc.flush(timeout=1)
 | 
						|
 | 
						|
</code></pre>
 | 
						|
	</div>
 | 
						|
	
 | 
						|
	<div class="tab__content">
 | 
						|
	<pre id="flush_ruby_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ruby-nats-examples/blob/master/flush.rb#L1-18"><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("updates") do |msg|
 | 
						|
    puts msg
 | 
						|
  end
 | 
						|
 | 
						|
  nc.publish("updates", "All is Well")
 | 
						|
 | 
						|
  nc.flush do
 | 
						|
    # Sends a PING and wait for a PONG from the server, up to the given timeout.
 | 
						|
    # This gives guarantee that the server has processed above message at this point.
 | 
						|
  end
 | 
						|
end
 | 
						|
 | 
						|
</code></pre>
 | 
						|
	</div>
 | 
						|
	
 | 
						|
	<div class="tab__content">
 | 
						|
	<pre id="flush_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#L87-105"><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 = await connect({
 | 
						|
    url: "nats://demo.nats.io:4222"
 | 
						|
});
 | 
						|
 | 
						|
// you can use flush to trigger a function in your
 | 
						|
// application once the round-trip to the server finishes
 | 
						|
let start = Date.now();
 | 
						|
nc.flush(() => {
 | 
						|
    t.log('round trip completed in', Date.now() - start, 'ms');
 | 
						|
});
 | 
						|
 | 
						|
nc.publish('foo');
 | 
						|
 | 
						|
// another way, simply wait for the promise to resolve
 | 
						|
await nc.flush();
 | 
						|
 | 
						|
nc.close();
 | 
						|
</code></pre>
 | 
						|
	</div>
 | 
						|
	
 | 
						|
</div>
 |