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

143 lines
6.4 KiB
HTML

<div class="tab-wrap">
<input type="radio" id="reconnect_event_go" name="reconnect_event" class="tab" checked>
<label for="reconnect_event_go" class="api-lang" data-language="go">Go</label>
<input type="radio" id="reconnect_event_java" name="reconnect_event" class="tab">
<label for="reconnect_event_java" class="api-lang" data-language="java">Java</label>
<input type="radio" id="reconnect_event_js" name="reconnect_event" class="tab">
<label for="reconnect_event_js" class="api-lang" data-language="js">JavaScript</label>
<input type="radio" id="reconnect_event_py" name="reconnect_event" class="tab">
<label for="reconnect_event_py" class="api-lang" data-language="py">Python</label>
<input type="radio" id="reconnect_event_ruby" name="reconnect_event" class="tab">
<label for="reconnect_event_ruby" class="api-lang" data-language="ruby">Ruby</label>
<input type="radio" id="reconnect_event_ts" name="reconnect_event" class="tab">
<label for="reconnect_event_ts" class="api-lang" data-language="ts">TypeScript</label>
<div class="tab__content">
<pre id="reconnect_event_go_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/go-nats-examples/blob/master/api-examples/reconnect_event/main.go#L10-28"><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">// Connection event handlers are invoked asynchronously
// and the state of the connection may have changed when
// the callback is invoked.
nc, err := nats.Connect(&#34;demo.nats.io&#34;,
nats.DisconnectHandler(func(nc *nats.Conn) {
// handle disconnect event
}),
nats.ReconnectHandler(func(nc *nats.Conn) {
// handle reconnect event
}))
if err != nil {
log.Fatal(err)
}
defer nc.Close()
// Do something with the connection
</code></pre>
</div>
<div class="tab__content">
<pre id="reconnect_event_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/ReconnectEvents.java#L12-28"><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">Options options = new Options.Builder().
server(&#34;nats://demo.nats.io:4222&#34;).
connectionListener((conn, type) -&gt; {
if (type == Events.RECONNECTED) {
// handle reconnected
} else if (type == Events.DISCONNECTED) {
// handle disconnected, wait for reconnect
}
}).
build();
Connection nc = Nats.connect(options);
// Do something with the connection
nc.close();
</code></pre>
</div>
<div class="tab__content">
<pre id="reconnect_event_js_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/node-nats-examples/blob/master/src/reconnect_samples.js#L74-83"><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({
maxReconnectAttempts: 10,
servers: [&#34;nats://demo.nats.io:4222&#34;]
});
nc.on(&#39;reconnect&#39;, (c) =&gt; {
console.log(&#39;reconnected&#39;);
});
</code></pre>
</div>
<div class="tab__content">
<pre id="reconnect_event_py_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/asyncio-nats-examples/blob/master/reconnect_event.py#L6-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 disconnected_cb():
print(&#34;Got disconnected!&#34;)
async def reconnected_cb():
# See who we are connected to on reconnect.
print(&#34;Got reconnected to {url}&#34;.format(url=nc.connected_url.netloc))
await nc.connect(
servers=[&#34;nats://demo.nats.io:4222&#34;],
reconnect_time_wait=10,
reconnected_cb=reconnected_cb,
disconnected_cb=disconnected_cb,
)
# Do something with the connection.
</code></pre>
</div>
<div class="tab__content">
<pre id="reconnect_event_ruby_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ruby-nats-examples/blob/master/reconnect_event.rb#L1-14"><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;
NATS.start(servers: [&#34;nats://127.0.0.1:1222&#34;, &#34;nats://127.0.0.1:1223&#34;, &#34;nats://127.0.0.1:1224&#34;]) do |nc|
# Do something with the connection
nc.on_reconnect do
puts &#34;Got reconnected to #{nc.connected_server}&#34;
end
nc.on_disconnect do |reason|
puts &#34;Got disconnected! #{reason}&#34;
end
end
</code></pre>
</div>
<div class="tab__content">
<pre id="reconnect_event_ts_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ts-nats-examples/blob/master/src/reconnect_samples.ts#L57-70"><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">// will throw an exception if connection fails
let nc = await connect({
maxReconnectAttempts: 10,
servers: [&#34;nats://demo.nats.io:4222&#34;]
});
// first argument is the connection (same as nc in this case)
// second argument is the url of the server where the client
// connected
nc.on(&#39;reconnect&#39;, (conn, server) =&gt; {
console.log(&#39;reconnected to&#39;, server);
});
nc.close();
</code></pre>
</div>
</div>