Documentation ¶
Overview ¶
Package mqtt provides a simple MQTT client wrapper around the paho.mqtt lib.
Index ¶
- Constants
- type Broker
- func (b *Broker) Build() Option
- func (b *Broker) WithHost(host string) *Broker
- func (b *Broker) WithPort(port int) *Broker
- func (b *Broker) WithProtocol(protocol protocol) *Broker
- func (b *Broker) WithSkipVerification() *Broker
- func (b *Broker) WithTlsCertificates(caPemPath, clientCrtPath, clientKeyPath string) *Broker
- func (b *Broker) WithUsernameAndPassword(username, password string) *Broker
- type Client
- type Message
- type Option
- type StatusPublishMessage
- type StatusType
Constants ¶
const ( MqttTcp = "mqtt://" MqttsTcp = "mqtts://" Ws = "ws://" Wss = "wss://" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Broker ¶
type Broker struct {
// contains filtered or unexported fields
}
Broker defines the properties ,which are needed to build the broker host and the TLS connection
func NewBrokerBuilder ¶
func NewBrokerBuilder() *Broker
NewBrokerBuilder configures the connection parameters like:
- Host, port, protocol
- TLS connection
func (*Broker) Build ¶
Build should be called, when all configuration is done. It creates an Option type, which is used in NewClient function.
func (*Broker) WithProtocol ¶
WithProtocol defines the protocol to use. There are these two options:
- tcp (normal - mqtt:// or secured - mqtts://)
- websocket (normal - ws:// or secured - wss://)
Provides an autocorrection, when WithTlsCertificates is set.
func (*Broker) WithSkipVerification ¶
WithSkipVerification the client will skip validating the cert chain and the hostname
see: [crypto/tls.Config.skipVerification]
func (*Broker) WithTlsCertificates ¶
WithTlsCertificates uses TLS and certificate auth for the connection.
- caPemPath points to the MQTT servers root CA
- clientCrtPath points to the public cert of the client
- clientKeyPath points to the private key of the client
If either clientCrtPath or clientKeyPath is an empty string, nothing will be changed.
func (*Broker) WithUsernameAndPassword ¶
WithUsernameAndPassword defines the username and password, if the broker requires authentication.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client struct, which holds the client itself and the options for it.
func CreateClient
deprecated
func NewClient ¶
NewClient Creates a new MQTT client with default configuration. To override the configuration, use the predefined options ("withBroker", "withClientId" and "withOrderMatters") or customize them by providing the Option type as parameter.
func (*Client) Connect ¶
Connect connects to the mqtt broker. Return an error, if something went wrong.
func (*Client) Disconnect ¶
Disconnect does a clean disconnect from the mqtt broker.
type Message ¶
Message defines the default message which is received and sent by the mqtt client. Here are two fields:
- Topic: The topic to which the message should be sent
- Value: Any struct or type (simple string or a complex type)
func (Message) ToRawString ¶
ToRawString converts the received JSON string to an ordinary string
type Option ¶
type Option func(options *pahoMqtt.ClientOptions)
Option defines the function which overrides the default options of the mqtt client.
func WithBroker ¶
WithBroker defines the host and port of the mqtt broker. (default port usually is 1883)
func WithClientId ¶
WithClientId defines the client name which will be registered on the broker.
func WithOrderMatters ¶
WithOrderMatters defines if the order of publish and subscribed message is respected. -> it indicates that messages can be delivered asynchronously from the client to the application and possibly arrive out of order. See pahoMqtt.ClientOptions.Order and the "SetOrderMatters" function.
type StatusPublishMessage ¶
type StatusPublishMessage struct { Type StatusType `json:"type" binding:"required"` Status string `json:"status"` Timestamp time.Time `json:"timestamp"` }
StatusPublishMessage defines a possible message to represent a "health message"
type StatusType ¶
type StatusType string
StatusType can be used for a basic status message (like a "health message") with
const ( Info StatusType = "info" Error StatusType = "error" Warn StatusType = "warn" )
Default StatusType constants.