1
0
mirror of https://github.com/taigrr/nats.docs synced 2025-01-18 04:03:23 -08:00

GitBook: [master] 82 pages modified

This commit is contained in:
Ginger Collison
2019-12-18 22:09:45 +00:00
committed by gitbook-bot
parent 7e27f03c98
commit b082996143
71 changed files with 865 additions and 930 deletions

View File

@@ -9,7 +9,7 @@ The following example subscribes to the subject `updates` and handles the incomi
```go
nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
defer nc.Close()
@@ -19,9 +19,9 @@ wg.Add(1)
// Subscribe
if _, err := nc.Subscribe("updates", func(m *nats.Msg) {
wg.Done()
wg.Done()
}); err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Wait for a message to come in

View File

@@ -30,15 +30,15 @@ errCh := make(chan error, 1)
// so say: nats.DrainTimeout(10*time.Millisecond).
nc, err := nats.Connect("demo.nats.io",
nats.DrainTimeout(10*time.Second),
nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, err error) {
errCh <- err
}),
nats.ClosedHandler(func(_ *nats.Conn) {
wg.Done()
}))
nats.DrainTimeout(10*time.Second),
nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, err error) {
errCh <- err
}),
nats.ClosedHandler(func(_ *nats.Conn) {
wg.Done()
}))
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Just to not collide using the demo server with other users.
@@ -46,19 +46,19 @@ subject := nats.NewInbox()
// Subscribe, but add some delay while processing.
if _, err := nc.Subscribe(subject, func(_ *nats.Msg) {
time.Sleep(200 * time.Millisecond)
time.Sleep(200 * time.Millisecond)
}); err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Publish a message
if err := nc.Publish(subject, []byte("hello")); err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Drain the connection, which will close it when done.
if err := nc.Drain(); err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Wait for the connection to be closed.
@@ -67,7 +67,7 @@ wg.Wait()
// Check if there was an error
select {
case e := <-errCh:
log.Fatal(e)
log.Fatal(e)
default:
}
```
@@ -215,61 +215,60 @@ The API for drain can generally be used instead of unsubscribe:
{% tabs %}
{% tab title="Go" %}
```go
nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
}
defer nc.Close()
nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
}
defer nc.Close()
done := sync.WaitGroup{}
done.Add(1)
done := sync.WaitGroup{}
done.Add(1)
count := 0
errCh := make(chan error, 1)
count := 0
errCh := make(chan error, 1)
msgAfterDrain := "not this one"
msgAfterDrain := "not this one"
// Just to not collide using the demo server with other users.
subject := nats.NewInbox()
// Just to not collide using the demo server with other users.
subject := nats.NewInbox()
// This callback will process each message slowly
sub, err := nc.Subscribe(subject, func(m *nats.Msg) {
if string(m.Data) == msgAfterDrain {
errCh <- fmt.Errorf("Should not have received this message")
return
}
time.Sleep(100 * time.Millisecond)
count++
if count == 2 {
done.Done()
}
})
// This callback will process each message slowly
sub, err := nc.Subscribe(subject, func(m *nats.Msg) {
if string(m.Data) == msgAfterDrain {
errCh <- fmt.Errorf("Should not have received this message")
return
}
time.Sleep(100 * time.Millisecond)
count++
if count == 2 {
done.Done()
}
})
// Send 2 messages
for i := 0; i < 2; i++ {
nc.Publish(subject, []byte("hello"))
}
// Send 2 messages
for i := 0; i < 2; i++ {
nc.Publish(subject, []byte("hello"))
}
// Call Drain on the subscription. It unsubscribes but
// wait for all pending messages to be processed.
if err := sub.Drain(); err != nil {
log.Fatal(err)
}
// Call Drain on the subscription. It unsubscribes but
// wait for all pending messages to be processed.
if err := sub.Drain(); err != nil {
log.Fatal(err)
}
// Send one more message, this message should not be received
nc.Publish(subject, []byte(msgAfterDrain))
// Send one more message, this message should not be received
nc.Publish(subject, []byte(msgAfterDrain))
// Wait for the subscription to have processed the 2 messages.
done.Wait()
// Wait for the subscription to have processed the 2 messages.
done.Wait()
// Now check that the 3rd message was not received
select {
case e := <-errCh:
log.Fatal(e)
case <-time.After(200 * time.Millisecond):
// OK!
}
// Now check that the 3rd message was not received
select {
case e := <-errCh:
log.Fatal(e)
case <-time.After(200 * time.Millisecond):
// OK!
}
```
{% endtab %}

View File

@@ -13,7 +13,7 @@ As an example, to subscribe to the queue `workers` with the subject `updates`:
```go
nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
defer nc.Close()
@@ -23,9 +23,9 @@ wg.Add(10)
// Create a queue subscription on "updates" with queue name "workers"
if _, err := nc.QueueSubscribe("updates", "worker", func(m *nats.Msg) {
wg.Done()
wg.Done()
}); err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Wait for messages to come in
@@ -125,17 +125,17 @@ If you run this example with the publish examples that send to `updates`, you wi
## Queue Permissions
Added in NATS Server v2.1.2, Queue Permissions allow you to express authorization for queue groups. As queue groups are integral to implementing horizontally scalable microservices, control of who is allowed to join a specific queue group is important to the overall security model.
Added in NATS Server v2.1.2, Queue Permissions allow you to express authorization for queue groups. As queue groups are integral to implementing horizontally scalable microservices, control of who is allowed to join a specific queue group is important to the overall security model.
A Queue Permission can be defined with the syntax `<subject> <queue>`, where the name of the queue can also use wildcards, for example the following would allow clients to join queue groups v1 and v2.*, but won't allow plain subscriptions:
A Queue Permission can be defined with the syntax `<subject> <queue>`, where the name of the queue can also use wildcards, for example the following would allow clients to join queue groups v1 and v2.\*, but won't allow plain subscriptions:
```hcl
```text
allow = ["foo v1", "foo v2.*"]
```
The full wildcard can also be used, for example the following would prevent plain subscriptions on `bar` but allow the client to join any queue:
```
```text
allow = ["bar >"]
```
@@ -155,3 +155,4 @@ users = [
}
]
```

View File

@@ -9,20 +9,20 @@ For example, the following code will listen for that request and respond with th
```go
nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
defer nc.Close()
// Subscribe
sub, err := nc.SubscribeSync("time")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Read a message
msg, err := sub.NextMsg(10 * time.Second)
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Get the time
@@ -69,7 +69,6 @@ nc.subscribe('time', (msg, reply) => {
nc.publish(msg.reply, new Date().toLocaleTimeString());
}
});
```
{% endtab %}

View File

@@ -9,19 +9,19 @@ For example, to receive JSON you could do:
```go
nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
defer nc.Close()
ec, err := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
defer ec.Close()
// Define the object
type stock struct {
Symbol string
Price int
Symbol string
Price int
}
wg := sync.WaitGroup{}
@@ -29,10 +29,10 @@ wg.Add(1)
// Subscribe
if _, err := ec.Subscribe("updates", func(s *stock) {
log.Printf("Stock: %s - Price: %v", s.Symbol, s.Price)
wg.Done()
log.Printf("Stock: %s - Price: %v", s.Symbol, s.Price)
wg.Done()
}); err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Wait for a message to come in
@@ -66,7 +66,7 @@ public class SubscribeJSON {
String json = new String(msg.getData(), StandardCharsets.UTF_8);
StockForJsonSub stk = gson.fromJson(json, StockForJsonSub.class);
// Use the object
System.out.println(stk);

View File

@@ -9,20 +9,20 @@ For example, to subscribe to the subject `updates` and receive a single message
```go
nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
defer nc.Close()
// Subscribe
sub, err := nc.SubscribeSync("updates")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Wait for a message
msg, err := sub.NextMsg(10 * time.Second)
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Use the response

View File

@@ -17,26 +17,26 @@ The following example shows unsubscribe after a single message:
```go
nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
defer nc.Close()
// Sync Subscription
sub, err := nc.SubscribeSync("updates")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
if err := sub.AutoUnsubscribe(1); err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Async Subscription
sub, err = nc.Subscribe("updates", func(_ *nats.Msg) {})
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
if err := sub.AutoUnsubscribe(1); err != nil {
log.Fatal(err)
log.Fatal(err)
}
```
{% endtab %}
@@ -126,7 +126,6 @@ NATS.start(servers:["nats://127.0.0.1:4222"]) do |nc|
end.resume
end
```
{% endtab %}

View File

@@ -9,26 +9,26 @@ This process requires an interaction with the server, so for an asynchronous sub
```go
nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
defer nc.Close()
// Sync Subscription
sub, err := nc.SubscribeSync("updates")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
if err := sub.Unsubscribe(); err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Async Subscription
sub, err = nc.Subscribe("updates", func(_ *nats.Msg) {})
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
if err := sub.Unsubscribe(); err != nil {
log.Fatal(err)
log.Fatal(err)
}
```
{% endtab %}
@@ -92,7 +92,6 @@ await nc.unsubscribe(sid)
# Won't be received...
await nc.publish("updates", b'...')
```
{% endtab %}

View File

@@ -11,7 +11,7 @@ For example, you can subscribe using `*` and then act based on the actual subjec
```go
nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
defer nc.Close()
@@ -21,10 +21,10 @@ wg.Add(2)
// Subscribe
if _, err := nc.Subscribe("time.*.east", func(m *nats.Msg) {
log.Printf("%s: %s", m.Subject, m.Data)
wg.Done()
log.Printf("%s: %s", m.Subject, m.Data)
wg.Done()
}); err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Wait for the 2 messages to come in
@@ -176,7 +176,7 @@ or do something similar with `>`:
```go
nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
defer nc.Close()
@@ -186,10 +186,10 @@ wg.Add(4)
// Subscribe
if _, err := nc.Subscribe("time.>", func(m *nats.Msg) {
log.Printf("%s: %s", m.Subject, m.Data)
wg.Done()
log.Printf("%s: %s", m.Subject, m.Data)
wg.Done()
}); err != nil {
log.Fatal(err)
log.Fatal(err)
}
// Wait for the 4 messages to come in
@@ -346,13 +346,13 @@ The following example can be used to test these two subscribers. The `*` subscri
```go
nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
defer nc.Close()
zoneID, err := time.LoadLocation("America/New_York")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
now := time.Now()
zoneDateTime := now.In(zoneID)
@@ -363,14 +363,13 @@ nc.Publish("time.us.east.atlanta", []byte(formatted))
zoneID, err = time.LoadLocation("Europe/Warsaw")
if err != nil {
log.Fatal(err)
log.Fatal(err)
}
zoneDateTime = now.In(zoneID)
formatted = zoneDateTime.String()
nc.Publish("time.eu.east", []byte(formatted))
nc.Publish("time.eu.east.warsaw", []byte(formatted))
```
{% endtab %}
@@ -392,7 +391,6 @@ nc.publish("time.eu.east.warsaw", formatted.getBytes(StandardCharsets.UTF_8));
nc.flush(Duration.ZERO);
nc.close();
```
{% endtab %}
@@ -402,7 +400,6 @@ nc.publish('time.us.east');
nc.publish('time.us.central');
nc.publish('time.us.mountain');
nc.publish('time.us.west');
```
{% endtab %}
@@ -419,7 +416,6 @@ await nc.publish("time.eu.east", b'...')
await nc.publish("time.eu.east.warsaw", b'...')
await nc.close()
```
{% endtab %}
@@ -434,7 +430,6 @@ NATS.start do |nc|
nc.drain
end
```
{% endtab %}
@@ -444,7 +439,6 @@ nc.publish('time.us.east');
nc.publish('time.us.central');
nc.publish('time.us.mountain');
nc.publish('time.us.west');
```
{% endtab %}
{% endtabs %}