Documentation ¶
Index ¶
Constants ¶
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 ¶
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") } )
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 ¶
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
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