mqtt

package
v0.0.0-...-f8c6ae9 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	QOS = 1
)

this package can be used to define reading and writing routines that connect to the Mosquitto broker those routines are supposed to run in their own goroutines and either receive or pass events through channels

Variables

View Source
var (
	DefaultPublishHandler = func(client mqtt.Client, msg mqtt.Message) {
		fmt.Printf("unexpected message: %s\n", msg)
	}

	DefaultOnConnectionLostHandler = func(client mqtt.Client, err error) {
		fmt.Println("connection lost")
	}

	DefaultOnReconnectingHandler = func(client mqtt.Client, option *mqtt.ClientOptions) {
		fmt.Println("attempting to reconnect")
	}
)
View Source
var (
	Debug = false
)

Functions

This section is empty.

Types

type Message

type Message struct {
	Topic   string
	Payload interface{}
}

Message is a simple struct that allows to publish to different topics with a single publisher client connection

type Publisher

type Publisher struct {
	// contains filtered or unexported fields
}

Publisher wraps the mqtt client that is subscribed to a specific topic in a pretty simple to use manner. initially you connect to your broker and fetch reveived messages with the method Next(). Next() is a blocking call that waits for a channel to contain a message or until the Close() method has been called that cancels an internally wrapped context, which immediatly terminates

func NewPublisher

func NewPublisher(address, clientID, topic string) (*Publisher, error)

NewPublisher creates and starts a new Publisher that receives new messages via a string channel that can be address has the format: tcp://localhost:1883

func (*Publisher) Close

func (p *Publisher) Close()

Close waits a second and then closes the client connection as well as the subsciber and all internally used channels

func (*Publisher) Publish

func (p *Publisher) Publish(msg interface{})

Publish pushes the message into a channel which is emptied by a concurrent goroutine and published to th ebroker at the specified topic. use string, []byte, int, int64, float32, float64 or

Message{
	  Topic string,
   Payload inteface{}
}

allows you to control the taget topic if you are using custom structs, please convert them into JSON before passing them to this function

func (*Publisher) PublishTo

func (p *Publisher) PublishTo(topic string, msg interface{})

PublishTo allows to specify a different topic other than the default one.

type Subscriber

type Subscriber struct {
	// contains filtered or unexported fields
}

Subscriber wraps the mqtt client that is subscribed to a specific topic in a pretty simple to use manner. initially you connect to your broker and fetch reveived messages with the method Next(). Next() is a blocking call that waits for a channel to contain a message or until the Close() method has been called that cancels an internally wrapped context, which immediatly terminates

func NewSubscriber

func NewSubscriber(address, clientID string, topics ...string) (*Subscriber, error)

NewSubscriber creates and starts a new subscriber that receives new messages via a string channel that can be address has the format: tcp://localhost:1883

func (*Subscriber) Close

func (s *Subscriber) Close()

Close waits a second and then closes the client connection as well as the subsciber and all internally used channels

func (*Subscriber) Next

func (s *Subscriber) Next() <-chan Message

Next blocks until the next message from the broker is received the bool indicates that the subscriber was closed you can use this in a for loop until ok is false, preferrably in an own goroutine

Jump to

Keyboard shortcuts

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