1
0
mirror of https://github.com/taigrr/nats.docs synced 2025-01-18 04:03:23 -08:00
nats.docs/_examples/flush.html
2019-05-30 12:43:58 -05:00

133 lines
5.7 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-26"><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()
if err := nc.Publish(&#34;updates&#34;, []byte(&#34;All is Well&#34;)); 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 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(&#34;nats://demo.nats.io:4222&#34;);
nc.publish(&#34;updates&#34;, &#34;All is Well&#34;.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: &#34;nats://demo.nats.io:4222&#34;});
let start = Date.now();
nc.flush(() =&gt; {
t.log(&#39;round trip completed in&#39;, Date.now() - start, &#39;ms&#39;);
});
nc.publish(&#39;foo&#39;);
// 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=[&#34;nats://demo.nats.io:4222&#34;])
await nc.publish(&#34;updates&#34;, b&#39;All is Well&#39;)
# 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 &#39;nats/client&#39;
require &#39;fiber&#39;
NATS.start(servers:[&#34;nats://127.0.0.1:4222&#34;]) do |nc|
nc.subscribe(&#34;updates&#34;) do |msg|
puts msg
end
nc.publish(&#34;updates&#34;, &#34;All is Well&#34;)
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: &#34;nats://demo.nats.io:4222&#34;
});
// 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(() =&gt; {
t.log(&#39;round trip completed in&#39;, Date.now() - start, &#39;ms&#39;);
});
nc.publish(&#39;foo&#39;);
// another way, simply wait for the promise to resolve
await nc.flush();
nc.close();
</code></pre>
</div>
</div>