Skip to content

Commit

Permalink
Merge pull request hybridgroup#615 from wallyqs/nats-updates
Browse files Browse the repository at this point in the history
NATS Updates
  • Loading branch information
deadprogram authored Sep 15, 2018
2 parents d975c8f + c5b507a commit d30d28a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 62 deletions.
123 changes: 63 additions & 60 deletions platforms/nats/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

NATS is a lightweight messaging protocol perfect for your IoT/Robotics projects. It operates over TCP, offers a great number of features but an incredibly simple Pub Sub style model of communicating broadcast messages. NATS is blazingly fast as it is written in Go.

This repository contains the Gobot adaptor/drivers to connect to NATS servers. It uses the NATS Go Client available at https://github.com/nats-io/nats. The NATS project is maintained by Nats.io and sponsored by Apcera. Find more information on setting up a NATS server and its capability at http://nats.io/.
This repository contains the Gobot adaptor/drivers to connect to NATS servers. It uses the NATS Go Client available at https://github.com/nats-io/nats. The NATS project is a project part of the [CNCF](https://www.cncf.io/). Find more information on setting up a NATS server and its capability at http://nats.io/.

The NATS messaging protocol (http://www.nats.io/documentation/internals/nats-protocol-demo/) is really easy to work with and can be practiced by setting up a NATS server using Go or Docker. For information on setting up a server using the source code, visit https://github.com/nats-io/gnatsd. For information on the Docker image up on Docker Hub, see https://hub.docker.com/_/nats/. Getting the server set up is very easy. The server itself is Golang, can be built for different architectures and installs in a small footprint. This is an excellent way to get communications going between your IoT and Robotics projects.

Expand All @@ -16,85 +16,88 @@ go get -d -u gobot.io/x/gobot/...

## How to Use

Before running the example, make sure you have an NATS server running somewhere you can connect to
Before running the example, make sure you have an NATS server running somewhere you can connect to (for demo purposes you could also use the public endpoint `demo.nats.io:4222`).

```go
package main

import (
"fmt"
"time"
"fmt"
"time"

"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/nats"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/nats"
)

func main() {
natsAdaptor := nats.NewNatsAdaptor("nats", "localhost:4222", 1234)

work := func() {
natsAdaptor.On("hello", func(msg nats.Message) {
fmt.Println(subject)
})
natsAdaptor.On("hola", func(msg nats.Message) {
fmt.Println(subject)
})
data := []byte("o")
gobot.Every(1*time.Second, func() {
natsAdaptor.Publish("hello", data)
})
gobot.Every(5*time.Second, func() {
natsAdaptor.Publish("hola", data)
})
}

robot := gobot.NewRobot("natsBot",
[]gobot.Connection{natsAdaptor},
work,
)

robot.Start()
natsAdaptor := nats.NewAdaptor("nats://localhost:4222", 1234)

work := func() {
natsAdaptor.On("hello", func(msg nats.Message) {
fmt.Printf("[Received on %q] %s\n", msg.Subject, string(msg.Data))
})
natsAdaptor.On("hola", func(msg nats.Message) {
fmt.Printf("[Received on %q] %s\n", msg.Subject, string(msg.Data))
})

data := []byte("Hello Gobot!")
gobot.Every(1*time.Second, func() {
natsAdaptor.Publish("hello", data)
})
gobot.Every(5*time.Second, func() {
natsAdaptor.Publish("hola", data)
})
}

robot := gobot.NewRobot("natsBot",
[]gobot.Connection{natsAdaptor},
work,
)

robot.Start()
}
```

To run with TLS enabled, set the URL scheme prefix to tls://. Make sure the NATS server has TLS enabled and use the NATS option parameters to pass in the TLS settings to the adaptor. Refer to the github.com/nats-io/go-nats README for more TLS option parameters.
To run with TLS enabled, set the URL scheme prefix to `tls://`. Make sure the NATS server has TLS enabled and use the NATS option parameters to pass in the TLS settings to the adaptor. Refer to the [github.com/nats-io/go-nats](https://github.com/nats-io/go-nats) README for more TLS option parameters.

```go
package main

import (
"fmt"
"time"
natsio "github.com/nats-io/nats"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/nats"
"fmt"
"time"

natsio "github.com/nats-io/go-nats"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/nats"
)

func main() {
natsAdaptor := nats.NewNatsAdaptor("tls://localhost:4222", 1234, natsio.RootCAs("certs/ca.pem"))

work := func() {
natsAdaptor.On("hello", func(msg nats.Message) {
fmt.Println(subject)
})
natsAdaptor.On("hola", func(msg nats.Message) {
fmt.Println(subject)
})
data := []byte("o")
gobot.Every(1*time.Second, func() {
natsAdaptor.Publish("hello", data)
})
gobot.Every(5*time.Second, func() {
natsAdaptor.Publish("hola", data)
})
}

robot := gobot.NewRobot("natsBot",
[]gobot.Connection{natsAdaptor},
work,
)

robot.Start()
natsAdaptor := nats.NewAdaptor("tls://localhost:4222", 1234, natsio.RootCAs("certs/ca.pem"))

work := func() {
natsAdaptor.On("hello", func(msg nats.Message) {
fmt.Printf("[Received on %q] %s\n", msg.Subject, string(msg.Data))
})
natsAdaptor.On("hola", func(msg nats.Message) {
fmt.Printf("[Received on %q] %s\n", msg.Subject, string(msg.Data))
})

data := []byte("Hello Gobot!")
gobot.Every(1*time.Second, func() {
natsAdaptor.Publish("hello", data)
})
gobot.Every(5*time.Second, func() {
natsAdaptor.Publish("hola", data)
})
}

robot := gobot.NewRobot("natsBot",
[]gobot.Connection{natsAdaptor},
work,
)

robot.Start()
}
```

Expand Down
2 changes: 1 addition & 1 deletion platforms/nats/nats_adaptor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nats

import (
"github.com/nats-io/nats"
"github.com/nats-io/go-nats"
"gobot.io/x/gobot"
"net/url"
"strings"
Expand Down
2 changes: 1 addition & 1 deletion platforms/nats/nats_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"testing"

"github.com/nats-io/nats"
"github.com/nats-io/go-nats"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
Expand Down

0 comments on commit d30d28a

Please sign in to comment.