...

Package mqtt

import("mqtt")
Overview
Index

Overview ▾

Package mqtt provides MQTT subscriber and publisher implementations.

type Config

Config specifies the options on how to connect to a broker.

type Config struct {
    Addr         string
    Username     string
    Password     string
    ClientID     string
    TLS          *tls.Config
    CleanSession bool
}

func NewConfig

func NewConfig(uri string) (*Config, error)

NewConfig returns a configuration for the provided URI string. The URI should follow the format "mqtt[s]://[username][:password]@host.domain[:port]".

type Message

Message is an MQTT message.

type Message struct {
    // Topic of the message.
    Topic string
    // Bytes of the message payload.
    Bytes []byte
}

type Publisher

Publisher is a publisher of messages.

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

func NewPublisher

func NewPublisher(config *Config) (*Publisher, error)

NewPublisher returns a new publisher.

func (Publisher) Disconnect

func (c Publisher) Disconnect() error

Disconnect closes the connection to the broker.

func (Publisher) Ping

func (c Publisher) Ping() error

Ping sends a ping message.

func (*Publisher) Publish

func (pub *Publisher) Publish(message Message) error

Publish sends the message at most once (QoS 0).

func (*Publisher) PublishAtLeastOnce

func (pub *Publisher) PublishAtLeastOnce(message Message) error

PublishAtLeastOnce sends the message at least once (QoS 1).

func (*Publisher) PublishAtLeastOnceRetained

func (pub *Publisher) PublishAtLeastOnceRetained(message Message) error

PublishAtLeastOnceRetained sends the message at least once (QoS 1) and sets the retain flag to true.

func (*Publisher) PublishExactlyOnce

func (pub *Publisher) PublishExactlyOnce(message Message) error

PublishExactlyOnce sends the message exactly once (QoS 2).

func (*Publisher) PublishExactlyOnceRetained

func (pub *Publisher) PublishExactlyOnceRetained(message Message) error

PublishExactlyOnceRetained sends the message exactly once (QoS 2) and sets the retain flag to true.

func (*Publisher) PublishRetained

func (pub *Publisher) PublishRetained(message Message) error

PublishRetained sends the message at most once (QoS 0) and sets the retain flag to true.

type Subscriber

Subscriber can subscribe to topics.

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

func NewSubscriber

func NewSubscriber(config *Config) (*Subscriber, error)

NewSubscriber returns a subscriber.

func (*Subscriber) C

func (sub *Subscriber) C() <-chan *Message

C returns a channel through which the incoming messages from topic subscription are delivered. Use the `Subscribe()` to subscribe to one or more topics.

If the connection to the broker is closed, the channel returns a nil message. Use the value of Err() to know why the connection is closed.

func (Subscriber) Disconnect

func (c Subscriber) Disconnect() error

Disconnect closes the connection to the broker.

func (*Subscriber) Err

func (sub *Subscriber) Err() error

Err returns a non-nil error explaing why the connection to the broker is closed. Err returns nil, if the connection is still up.

func (Subscriber) Ping

func (c Subscriber) Ping() error

Ping sends a ping message.

func (*Subscriber) Subscribe

func (sub *Subscriber) Subscribe(topics ...string) error

Subscribe sends a SUBSCRIBE message to the broker to subscribe to one or more topics. The incoming messages are delivered through the channel `C()`.