mirror of
https://github.com/taigrr/nats.docs
synced 2025-01-18 04:03:23 -08:00
194 lines
7.9 KiB
HTML
194 lines
7.9 KiB
HTML
|
|
<div class="tab-wrap">
|
|
|
|
|
|
<input type="radio" id="subscribe_star_go" name="subscribe_star" class="tab" checked>
|
|
|
|
<label for="subscribe_star_go" class="api-lang" data-language="go">Go</label>
|
|
|
|
|
|
<input type="radio" id="subscribe_star_java" name="subscribe_star" class="tab">
|
|
|
|
<label for="subscribe_star_java" class="api-lang" data-language="java">Java</label>
|
|
|
|
|
|
<input type="radio" id="subscribe_star_js" name="subscribe_star" class="tab">
|
|
|
|
<label for="subscribe_star_js" class="api-lang" data-language="js">JavaScript</label>
|
|
|
|
|
|
<input type="radio" id="subscribe_star_py" name="subscribe_star" class="tab">
|
|
|
|
<label for="subscribe_star_py" class="api-lang" data-language="py">Python</label>
|
|
|
|
|
|
<input type="radio" id="subscribe_star_ruby" name="subscribe_star" class="tab">
|
|
|
|
<label for="subscribe_star_ruby" class="api-lang" data-language="ruby">Ruby</label>
|
|
|
|
|
|
<input type="radio" id="subscribe_star_ts" name="subscribe_star" class="tab">
|
|
|
|
<label for="subscribe_star_ts" class="api-lang" data-language="ts">TypeScript</label>
|
|
|
|
|
|
|
|
<div class="tab__content">
|
|
<pre id="subscribe_star_go_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/go-nats-examples/blob/master/api-examples/subscribe_star/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 2 messages to arrive
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(2)
|
|
|
|
// Subscribe
|
|
if _, err := nc.Subscribe("time.*.east", func(m *nats.Msg) {
|
|
log.Printf("%s: %s", m.Subject, m.Data)
|
|
wg.Done()
|
|
}); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Wait for the 2 messages to come in
|
|
wg.Wait()
|
|
|
|
// Close the connection
|
|
nc.Close()
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="subscribe_star_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/SubscribeStar.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 2 messages to arrive
|
|
CountDownLatch latch = new CountDownLatch(2);
|
|
|
|
// 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.*.east");
|
|
|
|
// Wait for messages to come in
|
|
latch.await();
|
|
|
|
// Close the connection
|
|
nc.close();
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="subscribe_star_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#L134-157"><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.us.*', (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_star_py_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/asyncio-nats-examples/blob/master/subscribe_star.py#L6-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-python">nc = NATS()
|
|
|
|
await nc.connect(servers=["nats://demo.nats.io:4222"])
|
|
|
|
# Use queue to wait for 2 messages to arrive
|
|
queue = asyncio.Queue()
|
|
async def cb(msg):
|
|
await queue.put_nowait(msg)
|
|
|
|
await nc.subscribe("time.*.east", 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')
|
|
|
|
msg_A = await queue.get()
|
|
msg_B = await queue.get()
|
|
|
|
print("Msg A:", msg_A)
|
|
print("Msg B:", msg_B)
|
|
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="subscribe_star_ruby_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ruby-nats-examples/blob/master/subscribe_star.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.*.east") do |msg, reply|
|
|
f.resume Time.now
|
|
end
|
|
|
|
nc.publish("time.A.east", "A")
|
|
nc.publish("time.B.east", "B")
|
|
|
|
# Use the response
|
|
msg_A = Fiber.yield
|
|
puts "Msg A: #{msg_A}"
|
|
|
|
msg_B = Fiber.yield
|
|
puts "Msg B: #{msg_B}"
|
|
|
|
end.resume
|
|
end
|
|
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="subscribe_star_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#L119-142"><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.us.*', (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";
|
|
}
|
|
console.log(msg.subject, time);
|
|
});
|
|
</code></pre>
|
|
</div>
|
|
|
|
</div>
|