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

190 lines
7.0 KiB
HTML

<div class="tab-wrap">
<input type="radio" id="subscribe_json_go" name="subscribe_json" class="tab" checked>
<label for="subscribe_json_go" class="api-lang" data-language="go">Go</label>
<input type="radio" id="subscribe_json_java" name="subscribe_json" class="tab">
<label for="subscribe_json_java" class="api-lang" data-language="java">Java</label>
<input type="radio" id="subscribe_json_js" name="subscribe_json" class="tab">
<label for="subscribe_json_js" class="api-lang" data-language="js">JavaScript</label>
<input type="radio" id="subscribe_json_py" name="subscribe_json" class="tab">
<label for="subscribe_json_py" class="api-lang" data-language="py">Python</label>
<input type="radio" id="subscribe_json_ruby" name="subscribe_json" class="tab">
<label for="subscribe_json_ruby" class="api-lang" data-language="ruby">Ruby</label>
<input type="radio" id="subscribe_json_ts" name="subscribe_json" class="tab">
<label for="subscribe_json_ts" class="api-lang" data-language="ts">TypeScript</label>
<div class="tab__content">
<pre id="subscribe_json_go_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/go-nats-examples/blob/master/api-examples/subscribe_json/main.go#L11-45"><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()
ec, err := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
if err != nil {
log.Fatal(err)
}
defer ec.Close()
// Define the object
type stock struct {
Symbol string
Price int
}
wg := sync.WaitGroup{}
wg.Add(1)
// Subscribe
if _, err := ec.Subscribe(&#34;updates&#34;, func(s *stock) {
log.Printf(&#34;Stock: %s - Price: %v&#34;, s.Symbol, s.Price)
wg.Done()
}); err != nil {
log.Fatal(err)
}
// Wait for a message to come in
wg.Wait()
// Close the connection
ec.Close()
</code></pre>
</div>
<div class="tab__content">
<pre id="subscribe_json_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/SubscribeJSON.java#L12-57"><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">class StockForJsonSub {
public String symbol;
public float price;
public String toString() {
return symbol &#43; &#34; is at &#34; &#43; price;
}
}
public class SubscribeJSON {
public static void main(String[] args) {
try {
Connection nc = Nats.connect(&#34;nats://demo.nats.io:4222&#34;);
// Use a latch to wait for 10 messages to arrive
CountDownLatch latch = new CountDownLatch(10);
// Create a dispatcher and inline message handler
Dispatcher d = nc.createDispatcher((msg) -&gt; {
Gson gson = new Gson();
String json = new String(msg.getData(), StandardCharsets.UTF_8);
StockForJsonSub stk = gson.fromJson(json, StockForJsonSub.class);
// Use the object
System.out.println(stk);
latch.countDown();
});
// Subscribe
d.subscribe(&#34;updates&#34;);
// Wait for a message to come in
latch.await();
// Close the connection
nc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
</code></pre>
</div>
<div class="tab__content">
<pre id="subscribe_json_js_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/node-nats-examples/blob/master/src/subscriber_samples.js#L76-88"><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;,
json: true
});
nc.subscribe(&#39;updates&#39;, (msg) =&gt; {
if(msg &amp;&amp; msg.ticker === &#39;TSLA&#39;) {
t.log(&#39;got message:&#39;, msg);
}
});
</code></pre>
</div>
<div class="tab__content">
<pre id="subscribe_json_py_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/asyncio-nats-examples/blob/master/subscribe_json.py#L1-23"><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">import asyncio
import json
from nats.aio.client import Client as NATS
from nats.aio.errors import ErrTimeout
async def run(loop):
nc = NATS()
await nc.connect(servers=[&#34;nats://127.0.0.1:4222&#34;], loop=loop)
async def message_handler(msg):
data = json.loads(msg.data.decode())
print(data)
sid = await nc.subscribe(&#34;updates&#34;, cb=message_handler)
await nc.flush()
await nc.auto_unsubscribe(sid, 2)
await nc.publish(&#34;updates&#34;, json.dumps({&#34;symbol&#34;: &#34;GOOG&#34;, &#34;price&#34;: 1200 }).encode())
await asyncio.sleep(1, loop=loop)
await nc.close()
</code></pre>
</div>
<div class="tab__content">
<pre id="subscribe_json_ruby_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ruby-nats-examples/blob/master/subscribe_json.rb#L1-13"><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;json&#39;
NATS.start(servers:[&#34;nats://127.0.0.1:4222&#34;]) do |nc|
nc.subscribe(&#34;updates&#34;) do |msg|
m = JSON.parse(msg)
# {&#34;symbol&#34;=&gt;&#34;GOOG&#34;, &#34;price&#34;=&gt;12}
p m
end
end
</code></pre>
</div>
<div class="tab__content">
<pre id="subscribe_json_ts_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ts-nats-examples/blob/master/src/subscriber_samples.ts#L70-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 = await connect({
url: &#34;nats://demo.nats.io:4222&#34;,
payload: Payload.JSON
});
nc.subscribe(&#39;updates&#39;, (err, msg) =&gt; {
t.log(&#39;got message:&#39;, msg.data ? msg.data : &#34;no payload&#34;);
});
</code></pre>
</div>
</div>