go_mqtt_client

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2022 License: MIT Imports: 4 Imported by: 1

README

mqttClient: a Go module

This is a go module to facilitate the use of MQTT clients by

  1. applying defaults
  2. handling errors
  3. printing helpful logs
  4. asynchronously publishing payloads

All of this allows for neater and non-repetitive code.

Customizing your broker

If you want to add or modify the broker options, you can access and modify MqttClient.Options before calling StartMqttClient. Example;

client := MqttClientInit(myHandlerFunction, mySubTopics, broker)
client.Options.SetClientID("myId")
client.StartMqttClient()

Tests

To run the tests simply execute go test from the root directory. Pass the -v flag to see all executed tests, not just the failed ones.

To see info and debug logs pass the --debug flag.

To see logs in a human friendly way pass the --human flag.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MqttClient

type MqttClient struct {
	LocalMqttClient mqtt.Client
	Options         *mqtt.ClientOptions
	Topics          []string
	// contains filtered or unexported fields
}

func MqttClientInit

func MqttClientInit(mqttMessageHandler mqtt.MessageHandler, subTopics []string, broker string) MqttClient

MqttClientInit returns an MqttClient initialised with the MessageHandler function given in mqttMessageHandler, the list of subscribed topics given in subTopics, the broker address given in broker, and default options for the KeepAlive (5 s), PingTimeout (1 s), AutoReconnect (false) and ClientId (a 4 byte hex string). These options can be changed by accessing the mqtt.ClientOptions structure from MqttClient.Options.

func (*MqttClient) Destroy

func (mc *MqttClient) Destroy()

Destroy disconnects from the broker.

func (*MqttClient) Publish

func (mc *MqttClient) Publish(payload interface{}, topic string)

Publish asynchronously publishes the payload on a topic. Prints out an error if it fails.

func (*MqttClient) Start

func (mc *MqttClient) Start() error

Start starts the MQTT client and subscribes to the list of topics given in MqttClientInit

func (*MqttClient) SubscribeToTopic

func (mc *MqttClient) SubscribeToTopic(topic string) error

SubscribeToTopic Subscribe to an additional topic. Ideally, the user should pass all the topics that they want to subscribe to MqttClientInit.

Jump to

Keyboard shortcuts

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