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:
committed by
gitbook-bot
parent
7e27f03c98
commit
b082996143
@@ -24,7 +24,7 @@ servers := []string{"nats://127.0.0.1:1222", "nats://127.0.0.1:1223", "nats://12
|
||||
|
||||
nc, err := nats.Connect(strings.Join(servers, ","))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ Each library has its own, language preferred way, to pass connection options. On
|
||||
```go
|
||||
nc, err := nats.Connect("demo.nats.io", nats.Name("API Options Example"), nats.Timeout(10*time.Second))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ Some libraries also provide a special way to connect to a _default_ url, which i
|
||||
```go
|
||||
nc, err := nats.Connect(nats.DefaultURL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ Keep in mind that each connection will have to turn off echo, and that it is per
|
||||
// Turn off echo
|
||||
nc, err := nats.Connect("demo.nats.io", nats.Name("API NoEcho Example"), nats.NoEcho())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ If you have a connection that is going to be open a long time with few messages
|
||||
// Set Ping Interval to 20 seconds
|
||||
nc, err := nats.Connect("demo.nats.io", nats.Name("API Ping Example"), nats.PingInterval(20*time.Second))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
@@ -99,14 +99,14 @@ For example, to set the maximum number of outgoing pings to 5:
|
||||
{% tab title="Go" %}
|
||||
```go
|
||||
// Set maximum number of PINGs out without getting a PONG back
|
||||
// before the connection will be disconnected as a stale connection.
|
||||
nc, err := nats.Connect("demo.nats.io", nats.Name("API MaxPing Example"), nats.MaxPingsOutstanding(5))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
// before the connection will be disconnected as a stale connection.
|
||||
nc, err := nats.Connect("demo.nats.io", nats.Name("API MaxPing Example"), nats.MaxPingsOutstanding(5))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
// Do something with the connection
|
||||
// Do something with the connection
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ While the client can't control the maximum payload size, clients may provide a w
|
||||
```go
|
||||
nc, err := nats.Connect("demo.nats.io")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
@@ -160,7 +160,7 @@ opts.Url = "demo.nats.io"
|
||||
opts.Pedantic = true
|
||||
nc, err := opts.Connect()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
@@ -245,7 +245,7 @@ opts.Url = "demo.nats.io"
|
||||
opts.Verbose = true
|
||||
nc, err := opts.Connect()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ For example, to connect to the demo server with a URL you can use:
|
||||
// nats.Connect("nats://demo.nats.io:4222")
|
||||
nc, err := nats.Connect("demo.nats.io")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -9,19 +9,19 @@ For example, the client library may provide a mechanism to get the connection's
|
||||
```go
|
||||
nc, err := nats.Connect("demo.nats.io", nats.Name("API Example"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
getStatusTxt := func(nc *nats.Conn) string {
|
||||
switch nc.Status() {
|
||||
case nats.CONNECTED:
|
||||
return "Connected"
|
||||
case nats.CLOSED:
|
||||
return "Closed"
|
||||
default:
|
||||
return "Other"
|
||||
}
|
||||
switch nc.Status() {
|
||||
case nats.CONNECTED:
|
||||
return "Connected"
|
||||
case nats.CLOSED:
|
||||
return "Closed"
|
||||
default:
|
||||
return "Other"
|
||||
}
|
||||
}
|
||||
log.Printf("The connection is %v\n", getStatusTxt(nc))
|
||||
|
||||
|
||||
@@ -166,12 +166,12 @@ When working with a cluster, servers may be added or changed. Some of the client
|
||||
// Be notified if a new server joins the cluster.
|
||||
// Print all the known servers and the only the ones that were discovered.
|
||||
nc, err := nats.Connect("demo.nats.io",
|
||||
nats.DiscoveredServersHandler(func(nc *nats.Conn) {
|
||||
log.Printf("Known servers: %v\n", nc.Servers())
|
||||
log.Printf("Discovered servers: %v\n", nc.DiscoveredServers())
|
||||
}))
|
||||
nats.DiscoveredServersHandler(func(nc *nats.Conn) {
|
||||
log.Printf("Known servers: %v\n", nc.Servers())
|
||||
log.Printf("Discovered servers: %v\n", nc.DiscoveredServers())
|
||||
}))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
@@ -251,11 +251,11 @@ The client library may separate server-to-client errors from events. Many server
|
||||
```go
|
||||
// Set the callback that will be invoked when an asynchronous error occurs.
|
||||
nc, err := nats.Connect("demo.nats.io",
|
||||
nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, err error) {
|
||||
log.Printf("Error: %v", err)
|
||||
}))
|
||||
nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, err error) {
|
||||
log.Printf("Error: %v", err)
|
||||
}))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -25,14 +25,14 @@ The first way that the incoming queue can be limited is by message count. The se
|
||||
```go
|
||||
nc, err := nats.Connect("demo.nats.io")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
// Subscribe
|
||||
sub1, err := nc.Subscribe("updates", func(m *nats.Msg) {})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Set limits of 1000 messages or 5MB, whichever comes first
|
||||
@@ -41,7 +41,7 @@ sub1.SetPendingLimits(1000, 5*1024*1024)
|
||||
// Subscribe
|
||||
sub2, err := nc.Subscribe("updates", func(m *nats.Msg) {})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Set no limits for this subscription
|
||||
@@ -124,7 +124,7 @@ Some libraries, like Java, will not send this notification on every dropped mess
|
||||
// Set the callback that will be invoked when an asynchronous error occurs.
|
||||
nc, err := nats.Connect("demo.nats.io", nats.ErrorHandler(logSlowConsumer))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
@@ -173,13 +173,11 @@ public class SlowConsumerListener {
|
||||
{% tab title="JavaScript" %}
|
||||
```javascript
|
||||
// slow consumer detection is not configurable on NATS JavaScript client.
|
||||
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
{% tab title="Python" %}
|
||||
```python
|
||||
|
||||
nc = NATS()
|
||||
|
||||
async def error_cb(e):
|
||||
@@ -202,7 +200,7 @@ public class SlowConsumerListener {
|
||||
|
||||
if len(msgs) == 3:
|
||||
# Head of line blocking on other messages caused
|
||||
# by single message proccesing taking long...
|
||||
# by single message proccesing taking long...
|
||||
await asyncio.sleep(1)
|
||||
|
||||
await nc.subscribe("updates", cb=cb, pending_msgs_limit=5)
|
||||
@@ -235,3 +233,4 @@ public class SlowConsumerListener {
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 %}
|
||||
|
||||
|
||||
@@ -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 = [
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
|
||||
@@ -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 %}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 %}
|
||||
|
||||
|
||||
@@ -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 %}
|
||||
|
||||
|
||||
@@ -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 %}
|
||||
|
||||
@@ -14,7 +14,7 @@ For clients that support this feature, you are able to configure the size of thi
|
||||
// Set reconnect buffer size in bytes (5 MB)
|
||||
nc, err := nats.Connect("demo.nats.io", nats.ReconnectBufSize(5*1024*1024))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
@@ -57,7 +57,6 @@ nc.close();
|
||||
{% tab title="TypeScript" %}
|
||||
```typescript
|
||||
// Reconnect buffer size is not configurable on NATS Typescript client
|
||||
|
||||
```
|
||||
{% endtab %}
|
||||
{% endtabs %}
|
||||
|
||||
@@ -8,7 +8,7 @@ You can disable automatic reconnect with connection options:
|
||||
// Disable reconnect attempts
|
||||
nc, err := nats.Connect("demo.nats.io", nats.NoReconnect())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -9,14 +9,14 @@ Because reconnect is primarily under the covers many libraries provide an event
|
||||
// and the state of the connection may have changed when
|
||||
// the callback is invoked.
|
||||
nc, err := nats.Connect("demo.nats.io",
|
||||
nats.DisconnectHandler(func(nc *nats.Conn) {
|
||||
// handle disconnect event
|
||||
}),
|
||||
nats.ReconnectHandler(func(nc *nats.Conn) {
|
||||
// handle reconnect event
|
||||
}))
|
||||
nats.DisconnectHandler(func(nc *nats.Conn) {
|
||||
// handle disconnect event
|
||||
}),
|
||||
nats.ReconnectHandler(func(nc *nats.Conn) {
|
||||
// handle reconnect event
|
||||
}))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ Applications can set the maximum reconnect attempts. Generally, this will limit
|
||||
// Set max reconnects attempts
|
||||
nc, err := nats.Connect("demo.nats.io", nats.MaxReconnects(10))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -8,13 +8,13 @@ However, if you want to disable the randomization process, so that servers are a
|
||||
{% tab title="Go" %}
|
||||
```go
|
||||
servers := []string{"nats://127.0.0.1:1222",
|
||||
"nats://127.0.0.1:1223",
|
||||
"nats://127.0.0.1:1224",
|
||||
"nats://127.0.0.1:1223",
|
||||
"nats://127.0.0.1:1224",
|
||||
}
|
||||
|
||||
nc, err := nats.Connect(strings.Join(servers, ","), nats.DontRandomize())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ It doesn’t make much sense to try to connect to the same server over and over.
|
||||
// Set reconnect interval to 10 seconds
|
||||
nc, err := nats.Connect("demo.nats.io", nats.ReconnectWait(10*time.Second))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ Given a creds file, a client can authenticate as a specific user belonging to a
|
||||
```go
|
||||
nc, err := nats.Connect("127.0.0.1", nats.UserCredentials("path_to_creds_file"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -9,11 +9,11 @@ Handling challenge response may require more than just a setting in the connecti
|
||||
```go
|
||||
opt, err := nats.NkeyOptionFromSeed("seed.txt")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
nc, err := nats.Connect("127.0.0.1", opt)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -18,10 +18,10 @@ Connecting to a server with TLS is straightforward. Most clients will automatica
|
||||
{% tab title="Go" %}
|
||||
```go
|
||||
nc, err := nats.Connect("localhost",
|
||||
nats.ClientCert("resources/certs/cert.pem", "resources/certs/key.pem"),
|
||||
nats.RootCAs("resources/certs/ca.pem"))
|
||||
nats.ClientCert("resources/certs/cert.pem", "resources/certs/key.pem"),
|
||||
nats.RootCAs("resources/certs/ca.pem"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
@@ -84,7 +84,7 @@ public class ConnectTLS {
|
||||
sslContext(ctx). // Set the SSL context
|
||||
build();
|
||||
Connection nc = Nats.connect(options);
|
||||
|
||||
|
||||
// Do something with the connection
|
||||
|
||||
nc.close();
|
||||
@@ -206,7 +206,7 @@ Some clients may support the `tls` protocol as well as a manual setting to turn
|
||||
```go
|
||||
nc, err := nats.Connect("tls://localhost", nats.RootCAs("resources/certs/ca.pem")) // May need this if server is using self-signed certificate
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
@@ -269,7 +269,7 @@ public class ConnectTLS {
|
||||
sslContext(ctx). // Set the SSL context
|
||||
build();
|
||||
Connection nc = Nats.connect(options);
|
||||
|
||||
|
||||
// Do something with the connection
|
||||
|
||||
nc.close();
|
||||
|
||||
@@ -18,7 +18,7 @@ The code uses localhost:4222 so that you can start the server on your machine to
|
||||
// Set a token
|
||||
nc, err := nats.Connect("127.0.0.1", nats.Name("API Token Example"), nats.Token("mytoken"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
@@ -85,7 +85,7 @@ Again, once you construct this URL you can connect as if this was a normal URL.
|
||||
// Token in URL
|
||||
nc, err := nats.Connect("mytoken@localhost")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ When logging in with a password `nats-server` will take either a plain text pass
|
||||
// Set a user and plain text password
|
||||
nc, err := nats.Connect("127.0.0.1", nats.UserInfo("myname", "password"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
@@ -109,7 +109,7 @@ Using this format, you can connect to a server using authentication as easily as
|
||||
// Set a user and plain text password
|
||||
nc, err := nats.Connect("myname:password@127.0.0.1")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
|
||||
@@ -9,12 +9,12 @@ All of the NATS clients are designed to make sending a message simple. For examp
|
||||
```go
|
||||
nc, err := nats.Connect("demo.nats.io", nats.Name("API PublishBytes Example"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
if err := nc.Publish("updates", []byte("All is Well")); err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
@@ -11,7 +11,7 @@ It is the libraries job to make sure messages flow in a high performance manner.
|
||||
```go
|
||||
nc, err := nats.Connect("demo.nats.io")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
@@ -19,12 +19,12 @@ defer nc.Close()
|
||||
subject := nats.NewInbox()
|
||||
|
||||
if err := nc.Publish(subject, []byte("All is Well")); err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Sends a PING and wait for a PONG from the server, up to the given timeout.
|
||||
// This gives guarantee that the server has processed the above message.
|
||||
if err := nc.FlushTimeout(time.Second); err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
@@ -7,7 +7,7 @@ The optional reply-to field when publishing a message can be used on the receivi
|
||||
```go
|
||||
nc, err := nats.Connect("demo.nats.io")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
@@ -17,19 +17,19 @@ uniqueReplyTo := nats.NewInbox()
|
||||
// Listen for a single response
|
||||
sub, err := nc.SubscribeSync(uniqueReplyTo)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Send the request.
|
||||
// If processing is synchronous, use Request() which returns the response message.
|
||||
if err := nc.PublishRequest("time", uniqueReplyTo, nil); err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Read the reply
|
||||
msg, err := sub.NextMsg(time.Second)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Use the response
|
||||
|
||||
@@ -13,14 +13,14 @@ For example, updating the previous publish example we may request `time` with a
|
||||
```go
|
||||
nc, err := nats.Connect("demo.nats.io")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
// Send the request
|
||||
msg, err := nc.Request("time", nil, time.Second)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Use the response
|
||||
|
||||
@@ -9,25 +9,25 @@ Take a simple _stock ticker_ that sends the symbol and price of each stock:
|
||||
```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
|
||||
}
|
||||
|
||||
// Publish the message
|
||||
if err := ec.Publish("updates", &stock{Symbol: "GOOG", Price: 1200}); err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
```
|
||||
{% endtab %}
|
||||
|
||||
Reference in New Issue
Block a user