2016-07-16 20:59:07 -07:00
|
|
|
# NATS
|
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
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.
|
2016-07-16 20:59:07 -07:00
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
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/>.
|
2016-07-16 20:59:07 -07:00
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
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.
|
2016-07-16 20:59:07 -07:00
|
|
|
|
|
|
|
## How to Install
|
|
|
|
|
2023-06-04 18:36:55 +02:00
|
|
|
Please refer to the main [README.md](https://github.com/hybridgroup/gobot/blob/release/README.md)
|
2016-07-16 20:59:07 -07:00
|
|
|
|
|
|
|
## How to Use
|
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
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`).
|
2016-07-16 20:59:07 -07:00
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-05-20 14:25:21 +02:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2016-07-16 20:59:07 -07:00
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/nats"
|
2016-07-16 20:59:07 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-05-20 14:25:21 +02:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2024-02-11 15:34:50 +01:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-07-16 20:59:07 -07:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
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.
|
2017-02-15 23:17:12 -10:00
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-05-20 14:25:21 +02:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2018-09-12 18:40:05 -07:00
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
natsio "github.com/nats-io/go-nats"
|
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/nats"
|
2017-02-15 23:17:12 -10:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-05-20 14:25:21 +02:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2024-02-11 15:34:50 +01:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-02-15 23:17:12 -10:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-12-19 19:16:50 +01:00
|
|
|
### Supported Features
|
2016-07-16 20:59:07 -07:00
|
|
|
|
|
|
|
* Publish messages
|
|
|
|
* Respond to incoming message events
|
2016-12-19 19:16:50 +01:00
|
|
|
* Support for Username/password authentication
|
2017-02-15 23:17:12 -10:00
|
|
|
* Support for NATS adaptor options to support TLS
|
2016-07-16 20:59:07 -07:00
|
|
|
|
2016-12-19 19:16:50 +01:00
|
|
|
### Upcoming Features
|
2016-07-16 20:59:07 -07:00
|
|
|
|
|
|
|
* Encoded messages (JSON)
|
|
|
|
* Exposing more NATS Features (tls)
|
|
|
|
* Simplified tests
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
2018-02-14 08:24:39 +01:00
|
|
|
Copyright (c) 2013-2018 The Hybrid Group. Licensed under the Apache 2.0 license.
|