nats

package
v1.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2017 License: Apache-2.0 Imports: 4 Imported by: 0

README

NATS

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/.

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.

How to Install

Install running:

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

package main

import (
  "fmt"
  "time"

  "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()
}

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.

package main

import (
  "fmt"
  "time"
  natsio "github.com/nats-io/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()
}
Supported Features
  • Publish messages
  • Respond to incoming message events
  • Support for Username/password authentication
  • Support for NATS adaptor options to support TLS
Upcoming Features
  • Encoded messages (JSON)
  • Exposing more NATS Features (tls)
  • Simplified tests

Contributing

For our contribution guidelines, please go to https://gobot.io/x/gobot/blob/master/CONTRIBUTING.md

License

Copyright (c) 2013-2017 The Hybrid Group. Licensed under the Apache 2.0 license.

Documentation

Overview

Package nats provides Gobot adaptor for the nats message service. Installing:

go get gobot.io/x/gobot/platforms/nats

For further information refer to mqtt README: https://github.com/hybridgroup/gobot/blob/master/platforms/nats/README.md

Index

Constants

View Source
const (
	// Data event when data is available for Driver
	Data = "data"

	// Error event when error occurs in Driver
	Error = "error"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Adaptor added in v1.0.0

type Adaptor struct {
	Host string
	// contains filtered or unexported fields
}

Adaptor is a configuration struct for interacting with a NATS server. Name is a logical name for the adaptor/nats server connection. Host is in the form "localhost:4222" which is the hostname/ip and port of the nats server. ClientID is a unique identifier integer that specifies the identity of the client.

func NewAdaptor added in v1.0.0

func NewAdaptor(host string, clientID int, options ...nats.Option) *Adaptor

NewAdaptor populates a new NATS Adaptor.

func NewAdaptorWithAuth added in v1.0.0

func NewAdaptorWithAuth(host string, clientID int, username string, password string, options ...nats.Option) *Adaptor

NewAdaptorWithAuth populates a NATS Adaptor including username and password.

func (*Adaptor) Connect added in v1.0.0

func (a *Adaptor) Connect() (err error)

Connect makes a connection to the Nats server.

func (*Adaptor) Disconnect added in v1.0.0

func (a *Adaptor) Disconnect() (err error)

Disconnect from the nats server.

func (*Adaptor) Finalize added in v1.0.0

func (a *Adaptor) Finalize() (err error)

Finalize is simply a helper method for the disconnect.

func (*Adaptor) Name added in v1.0.0

func (a *Adaptor) Name() string

Name returns the logical client name.

func (*Adaptor) On added in v1.0.0

func (a *Adaptor) On(event string, f func(msg Message)) bool

On is an event-handler style subscriber to a particular topic (named event). Supply a handler function to use the bytes returned by the server.

func (*Adaptor) Publish added in v1.0.0

func (a *Adaptor) Publish(topic string, message []byte) bool

Publish sends a message with the particular topic to the nats server.

func (*Adaptor) SetName added in v1.0.0

func (a *Adaptor) SetName(n string)

SetName sets the logical client name.

type Driver added in v1.2.0

type Driver struct {
	gobot.Eventer
	gobot.Commander
	// contains filtered or unexported fields
}

Driver for NATS

func NewDriver added in v1.2.0

func NewDriver(a *Adaptor, topic string) *Driver

NewDriver returns a new Gobot NATS Driver

func (*Driver) Connection added in v1.2.0

func (m *Driver) Connection() gobot.Connection

Connection returns Connections used by the Driver

func (*Driver) Halt added in v1.2.0

func (m *Driver) Halt() error

Halt halts the Driver

func (*Driver) Name added in v1.2.0

func (m *Driver) Name() string

Name returns name for the Driver

func (*Driver) On added in v1.2.0

func (m *Driver) On(n string, f func(msg Message)) error

On subscribes to data updates for the current device topic, and then calls the message handler function when data is received

func (*Driver) Publish added in v1.2.0

func (m *Driver) Publish(data interface{}) bool

Publish a message to the current device topic

func (*Driver) SetName added in v1.2.0

func (m *Driver) SetName(name string)

Name sets name for the Driver

func (*Driver) SetTopic added in v1.2.0

func (m *Driver) SetTopic(topic string)

SetTopic sets the current topic for the Driver

func (*Driver) Start added in v1.2.0

func (m *Driver) Start() error

Start starts the Driver

func (*Driver) Topic added in v1.2.0

func (m *Driver) Topic() string

Topic returns the current topic for the Driver

type Message added in v1.5.0

type Message *nats.Msg

Message is a message received from the server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL