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
@@ -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