mirror of
https://github.com/taigrr/nats.docs
synced 2025-01-18 04:03:23 -08:00
Update async.md
This commit is contained in:
parent
c23668e813
commit
af5236df8c
@ -4,5 +4,113 @@ Asynchronous subscriptions use callbacks of some form to notify an application w
|
||||
|
||||
The following example subscribes to the subject `updates` and handles the incoming messages:
|
||||
|
||||
!INCLUDE "../../\_examples/subscribe\_async.html"
|
||||
{% tabs %}
|
||||
{% tab title="Go" %}
|
||||
```go
|
||||
nc, err := nats.Connect("demo.nats.io")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
// Use a WaitGroup to wait for a message to arrive
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
|
||||
// Subscribe
|
||||
if _, err := nc.Subscribe("updates", func(m *nats.Msg) {
|
||||
wg.Done()
|
||||
}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Wait for a message to come in
|
||||
wg.Wait()
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="Java" %}
|
||||
```java
|
||||
Connection nc = Nats.connect("nats://demo.nats.io:4222");
|
||||
|
||||
// Use a latch to wait for a message to arrive
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
// Create a dispatcher and inline message handler
|
||||
Dispatcher d = nc.createDispatcher((msg) -> {
|
||||
String str = new String(msg.getData(), StandardCharsets.UTF_8);
|
||||
System.out.println(str);
|
||||
latch.countDown();
|
||||
});
|
||||
|
||||
// Subscribe
|
||||
d.subscribe("updates");
|
||||
|
||||
// Wait for a message to come in
|
||||
latch.await();
|
||||
|
||||
// Close the connection
|
||||
nc.close();
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="JavaScript" %}
|
||||
```javascript
|
||||
let nc = NATS.connect({
|
||||
url: "nats://demo.nats.io:4222"
|
||||
});
|
||||
nc.subscribe("updates", (msg) => {
|
||||
t.log(msg);
|
||||
});
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="Python" %}
|
||||
```python
|
||||
nc = NATS()
|
||||
|
||||
await nc.connect(servers=["nats://demo.nats.io:4222"])
|
||||
|
||||
future = asyncio.Future()
|
||||
|
||||
async def cb(msg):
|
||||
nonlocal future
|
||||
future.set_result(msg)
|
||||
|
||||
await nc.subscribe("updates", cb=cb)
|
||||
await nc.publish("updates", b'All is Well')
|
||||
await nc.flush()
|
||||
|
||||
# Wait for message to come in
|
||||
msg = await asyncio.wait_for(future, 1)
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="Ruby" %}
|
||||
```ruby
|
||||
require 'nats/client'
|
||||
|
||||
NATS.start(servers:["nats://127.0.0.1:4222"]) do |nc|
|
||||
nc.subscribe("updates") do |msg|
|
||||
puts msg
|
||||
nc.close
|
||||
end
|
||||
|
||||
nc.publish("updates", "All is Well")
|
||||
end
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="TypeScript" %}
|
||||
```typescript
|
||||
nc.subscribe("updates", (err, msg) => {
|
||||
if(err) {
|
||||
console.log('error', err);
|
||||
} else {
|
||||
t.log(msg.data);
|
||||
}
|
||||
});
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user