mqtt

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package mqtt provides a simple MQTT client wrapper around the paho.mqtt lib.

Index

Constants

View Source
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

func (b *Broker) Build() Option

Build should be called, when all configuration is done. It creates an Option type, which is used in NewClient function.

func (*Broker) WithHost

func (b *Broker) WithHost(host string) *Broker

WithHost defines the host of the broker. Can be either a DNS or an IP.

func (*Broker) WithPort

func (b *Broker) WithPort(port int) *Broker

WithPort defines the port of the broker to connect to.

func (*Broker) WithProtocol

func (b *Broker) WithProtocol(protocol protocol) *Broker

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

func (b *Broker) WithSkipVerification() *Broker

WithSkipVerification the client will skip validating the cert chain and the hostname

see: [crypto/tls.Config.skipVerification]

func (*Broker) WithTlsCertificates

func (b *Broker) WithTlsCertificates(caPemPath, clientCrtPath, clientKeyPath string) *Broker

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

func (b *Broker) WithUsernameAndPassword(username, password string) *Broker

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 CreateClient(ip string, port int, clientId string) *Client

Deprecated: Use "NewClient" instead of this. Will be removed in future version.

func NewClient

func NewClient(options ...Option) *Client

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

func (c *Client) Connect() error

Connect connects to the mqtt broker. Return an error, if something went wrong.

func (*Client) Disconnect

func (c *Client) Disconnect() error

Disconnect does a clean disconnect from the mqtt broker.

func (*Client) LoopForever

func (c *Client) LoopForever()

LoopForever Halts the current thread.

func (*Client) Publish

func (c *Client) Publish(message *Message)

Publish sends the give message to the mqtt broker. It uses QOS 2 and waits until arrival confirmation.

func (*Client) Subscribe

func (c *Client) Subscribe(topic string, fn func(message Message))

Subscribe subscribes to a topic and execute a function on message arrival. Returns an error, if the client is not connected to a mqtt broker.

type Message

type Message struct {
	Topic string
	Value any
}

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) FromJson

func (m Message) FromJson() string

FromJson converts a JSON struct to a string.

func (Message) ToRawString

func (m Message) ToRawString() string

ToRawString converts the received JSON string to an ordinary string

func (Message) ToStruct

func (m Message) ToStruct(target any)

ToStruct converts the received JSON message to the given target struct

type Option

type Option func(options *pahoMqtt.ClientOptions)

Option defines the function which overrides the default options of the mqtt client.

func WithBroker

func WithBroker(ip string, port int) Option

WithBroker defines the host and port of the mqtt broker. (default port usually is 1883)

func WithClientId

func WithClientId(clientId string) Option

WithClientId defines the client name which will be registered on the broker.

func WithOrderMatters

func WithOrderMatters(orderMatters bool) Option

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL