mqtt
Index
- func NewConfig(uri string) (*Config, error)
- func NewPublisher(config *Config) (*Publisher, error)
- func NewSubscriber(config *Config) (*Subscriber, error)
- type Config
- type Message
- type Publisher
- func NewPublisher(config *Config) (*Publisher, error)
- func (c Publisher) Disconnect() error
- func (c Publisher) Ping() error
- func (pub *Publisher) Publish(message Message) error
- func (pub *Publisher) PublishAtLeastOnce(message Message) error
- func (pub *Publisher) PublishAtLeastOnceRetained(message Message) error
- func (pub *Publisher) PublishExactlyOnce(message Message) error
- func (pub *Publisher) PublishExactlyOnceRetained(message Message) error
- func (pub *Publisher) PublishRetained(message Message) error
- type Subscriber
Functions
func NewConfig
1func 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]".
This example demonstates how to create a new config from an uri string.
1cfg, _ := mqtt.NewConfig("mqtt://testuser:secret@broker.emqx.io:1883")
2println(cfg.Addr)
3println(cfg.Username)
4println(cfg.Password)
Output
broker.emqx.io:1883 testuser secret
func NewPublisher
1func NewPublisher(config *Config) (*Publisher, error)
NewPublisher returns a new publisher.
func NewSubscriber
1func NewSubscriber(config *Config) (*Subscriber, error)
NewSubscriber returns a subscriber.
Types
type Config
1type Config struct {
2 Addr string
3 Username string
4 Password string
5 ClientID string
6 TLS *tls.Config
7 CleanSession bool
8}
Config specifies the options on how to connect to a broker.
type Message
1type Message struct {
2 // Topic of the message.
3 Topic string
4 // Bytes of the message payload.
5 Bytes []byte
6}
Message is an MQTT message.
type Publisher
1type Publisher struct {
2}
Publisher sends messages to a broker.
func Disconnect
1func (c Publisher) Disconnect() error
Disconnect closes the connection to the broker.
func Publish
1func (pub *Publisher) Publish(message Message) error
Publish sends the message at most once (QoS 0).
func PublishAtLeastOnce
1func (pub *Publisher) PublishAtLeastOnce(message Message) error
PublishAtLeastOnce sends the message at least once (QoS 1).
func PublishAtLeastOnceRetained
1func (pub *Publisher) PublishAtLeastOnceRetained(message Message) error
PublishAtLeastOnceRetained sends the message at least once (QoS 1) and sets the retain flag to true.
func PublishExactlyOnce
1func (pub *Publisher) PublishExactlyOnce(message Message) error
PublishExactlyOnce sends the message exactly once (QoS 2).
func PublishExactlyOnceRetained
1func (pub *Publisher) PublishExactlyOnceRetained(message Message) error
PublishExactlyOnceRetained sends the message exactly once (QoS 2) and sets the retain flag to true.
func PublishRetained
1func (pub *Publisher) PublishRetained(message Message) error
PublishRetained sends the message at most once (QoS 0) and sets the retain flag to true.
type Subscriber
1type Subscriber struct {
2}
Subscriber subscribes to one or more topics and receives messages from those subscriptions.
func C
1func (sub *Subscriber) C() <-chan *Message
C returns a channel through which the incoming messages from topic subscription are delivered. Use `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 Disconnect
1func (c Subscriber) Disconnect() error
Disconnect closes the connection to the broker.
func Err
1func (sub *Subscriber) Err() error
Err returns a non-nil error explaining why the connection to the broker is closed. Err returns nil, if the connection is still up.
func Subscribe
1func (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()`.