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(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]".
Message is an MQTT message.
type Message struct { // Topic of the message. Topic string // Bytes of the message payload. Bytes []byte }
Publisher is a publisher of messages.
type Publisher struct {
// contains filtered or unexported fields
}
func NewPublisher(config *Config) (*Publisher, error)
NewPublisher returns a new publisher.
func (c Publisher) Disconnect() error
Disconnect closes the connection to the broker.
func (c Publisher) Ping() error
Ping sends a ping message.
func (pub *Publisher) Publish(message Message) error
Publish sends the message at most once (QoS 0).
func (pub *Publisher) PublishAtLeastOnce(message Message) error
PublishAtLeastOnce sends the message at least once (QoS 1).
func (pub *Publisher) PublishAtLeastOnceRetained(message Message) error
PublishAtLeastOnceRetained sends the message at least once (QoS 1) and sets the retain flag to true.
func (pub *Publisher) PublishExactlyOnce(message Message) error
PublishExactlyOnce sends the message exactly once (QoS 2).
func (pub *Publisher) PublishExactlyOnceRetained(message Message) error
PublishExactlyOnceRetained sends the message exactly once (QoS 2) and sets the retain flag to true.
func (pub *Publisher) PublishRetained(message Message) error
PublishRetained sends the message at most once (QoS 0) and sets the retain flag to true.
Subscriber can subscribe to topics.
type Subscriber struct {
// contains filtered or unexported fields
}
func NewSubscriber(config *Config) (*Subscriber, error)
NewSubscriber returns a subscriber.
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 (c Subscriber) Disconnect() error
Disconnect closes the connection to the broker.
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 (c Subscriber) Ping() error
Ping sends a ping message.
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()`.