mirror of
https://github.com/taigrr/nats.docs
synced 2025-01-18 04:03:23 -08:00
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
This commit is contained in:
195
_examples/subscribe_arrow.html
Normal file
195
_examples/subscribe_arrow.html
Normal file
@@ -0,0 +1,195 @@
|
||||
|
||||
<div class="tab-wrap">
|
||||
|
||||
|
||||
<input type="radio" id="subscribe_arrow_go" name="subscribe_arrow" class="tab" checked>
|
||||
|
||||
<label for="subscribe_arrow_go" class="api-lang" data-language="go">Go</label>
|
||||
|
||||
|
||||
<input type="radio" id="subscribe_arrow_java" name="subscribe_arrow" class="tab">
|
||||
|
||||
<label for="subscribe_arrow_java" class="api-lang" data-language="java">Java</label>
|
||||
|
||||
|
||||
<input type="radio" id="subscribe_arrow_js" name="subscribe_arrow" class="tab">
|
||||
|
||||
<label for="subscribe_arrow_js" class="api-lang" data-language="js">JavaScript</label>
|
||||
|
||||
|
||||
<input type="radio" id="subscribe_arrow_py" name="subscribe_arrow" class="tab">
|
||||
|
||||
<label for="subscribe_arrow_py" class="api-lang" data-language="py">Python</label>
|
||||
|
||||
|
||||
<input type="radio" id="subscribe_arrow_ruby" name="subscribe_arrow" class="tab">
|
||||
|
||||
<label for="subscribe_arrow_ruby" class="api-lang" data-language="ruby">Ruby</label>
|
||||
|
||||
|
||||
<input type="radio" id="subscribe_arrow_ts" name="subscribe_arrow" class="tab">
|
||||
|
||||
<label for="subscribe_arrow_ts" class="api-lang" data-language="ts">TypeScript</label>
|
||||
|
||||
|
||||
|
||||
<div class="tab__content">
|
||||
<pre id="subscribe_arrow_go_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/go-nats-examples/blob/master/api-examples/subscribe_arrow/main.go#L11-35"><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()
|
||||
|
||||
// Use a WaitGroup to wait for 4 messages to arrive
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(4)
|
||||
|
||||
// Subscribe
|
||||
if _, err := nc.Subscribe("time.>", func(m *nats.Msg) {
|
||||
log.Printf("%s: %s", m.Subject, m.Data)
|
||||
wg.Done()
|
||||
}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Wait for the 4 messages to come in
|
||||
wg.Wait()
|
||||
|
||||
// Close the connection
|
||||
nc.Close()
|
||||
</code></pre>
|
||||
</div>
|
||||
|
||||
<div class="tab__content">
|
||||
<pre id="subscribe_arrow_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/SubscribeArrow.java#L14-36"><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");
|
||||
|
||||
// Use a latch to wait for 4 messages to arrive
|
||||
CountDownLatch latch = new CountDownLatch(4);
|
||||
|
||||
// Create a dispatcher and inline message handler
|
||||
Dispatcher d = nc.createDispatcher((msg) -> {
|
||||
String subject = msg.getSubject();
|
||||
String str = new String(msg.getData(), StandardCharsets.UTF_8);
|
||||
System.out.println(subject + ": " + str);
|
||||
latch.countDown();
|
||||
});
|
||||
|
||||
// Subscribe
|
||||
d.subscribe("time.>");
|
||||
|
||||
// Wait for messages to come in
|
||||
latch.await();
|
||||
|
||||
// Close the connection
|
||||
nc.close();
|
||||
</code></pre>
|
||||
</div>
|
||||
|
||||
<div class="tab__content">
|
||||
<pre id="subscribe_arrow_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#L176-199"><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">nc.subscribe('time.>', (msg, reply, subject) => {
|
||||
// converting timezones correctly in node requires a library
|
||||
// this doesn't take into account *many* things.
|
||||
let time = "";
|
||||
switch (subject) {
|
||||
case 'time.us.east':
|
||||
time = new Date().toLocaleTimeString("en-us", {timeZone: "America/New_York"});
|
||||
break;
|
||||
case 'time.us.central':
|
||||
time = new Date().toLocaleTimeString("en-us", {timeZone: "America/Chicago"});
|
||||
break;
|
||||
case 'time.us.mountain':
|
||||
time = new Date().toLocaleTimeString("en-us", {timeZone: "America/Denver"});
|
||||
break;
|
||||
case 'time.us.west':
|
||||
time = new Date().toLocaleTimeString("en-us", {timeZone: "America/Los_Angeles"});
|
||||
break;
|
||||
default:
|
||||
time = "I don't know what you are talking about Willis";
|
||||
}
|
||||
t.log(subject, time);
|
||||
});
|
||||
</code></pre>
|
||||
</div>
|
||||
|
||||
<div class="tab__content">
|
||||
<pre id="subscribe_arrow_py_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/asyncio-nats-examples/blob/master/subscribe_arrow.py#L6-30"><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"])
|
||||
|
||||
# Use queue to wait for 4 messages to arrive
|
||||
queue = asyncio.Queue()
|
||||
async def cb(msg):
|
||||
await queue.put(msg)
|
||||
|
||||
await nc.subscribe("time.>", cb=cb)
|
||||
|
||||
# Send 2 messages and wait for them to come in
|
||||
await nc.publish("time.A.east", b'A')
|
||||
await nc.publish("time.B.east", b'B')
|
||||
await nc.publish("time.C.west", b'C')
|
||||
await nc.publish("time.D.west", b'D')
|
||||
|
||||
for i in range(0, 4):
|
||||
msg = await queue.get()
|
||||
print("Msg:", msg)
|
||||
|
||||
await nc.close()
|
||||
|
||||
</code></pre>
|
||||
</div>
|
||||
|
||||
<div class="tab__content">
|
||||
<pre id="subscribe_arrow_ruby_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ruby-nats-examples/blob/master/subscribe_arrow.rb#L1-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-ruby">require 'nats/client'
|
||||
require 'fiber'
|
||||
|
||||
NATS.start(servers:["nats://127.0.0.1:4222"]) do |nc|
|
||||
Fiber.new do
|
||||
f = Fiber.current
|
||||
|
||||
nc.subscribe("time.>") do |msg, reply|
|
||||
f.resume Time.now.to_f
|
||||
end
|
||||
|
||||
nc.publish("time.A.east", "A")
|
||||
nc.publish("time.B.east", "B")
|
||||
nc.publish("time.C.west", "C")
|
||||
nc.publish("time.D.west", "D")
|
||||
|
||||
# Use the response
|
||||
4.times do
|
||||
msg = Fiber.yield
|
||||
puts "Msg: #{msg}"
|
||||
end
|
||||
end.resume
|
||||
end
|
||||
|
||||
</code></pre>
|
||||
</div>
|
||||
|
||||
<div class="tab__content">
|
||||
<pre id="subscribe_arrow_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#L156-179"><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">await nc.subscribe('time.>', (err, msg) => {
|
||||
// converting timezones correctly in node requires a library
|
||||
// this doesn't take into account *many* things.
|
||||
let time = "";
|
||||
switch (msg.subject) {
|
||||
case 'time.us.east':
|
||||
time = new Date().toLocaleTimeString("en-us", {timeZone: "America/New_York"});
|
||||
break;
|
||||
case 'time.us.central':
|
||||
time = new Date().toLocaleTimeString("en-us", {timeZone: "America/Chicago"});
|
||||
break;
|
||||
case 'time.us.mountain':
|
||||
time = new Date().toLocaleTimeString("en-us", {timeZone: "America/Denver"});
|
||||
break;
|
||||
case 'time.us.west':
|
||||
time = new Date().toLocaleTimeString("en-us", {timeZone: "America/Los_Angeles"});
|
||||
break;
|
||||
default:
|
||||
time = "I don't know what you are talking about Willis";
|
||||
}
|
||||
t.log(msg.subject, time);
|
||||
});
|
||||
</code></pre>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
Reference in New Issue
Block a user