mqtt

package module
v1.0.1-0...-dab8db0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2021 License: MIT Imports: 15 Imported by: 0

README

GoDoc Go Report Card

MQTT-Wrapper

MQTT-Wrapper package provides easy-to-use MQTTv3 and MQTTv5 connection for Go projects.

It supports Request/Response pattern for MQTTv5 connection.

Usage

  • Import the package
import( 
    mqtt "github.com/alihanyalcin/mqtt-wrapper"
)
  • Create a local variable using MQTTConfig struct and fill the necessary fields for the connection.
config := mqtt.Config{
    Brokers:              []string{"127.0.0.1:1883"},
    ClientID:             "",
    Username:             "",
    Password:             "",
    Topics:               []string{"topic"},
    QoS:                  0,
    Retained:             false,
    AutoReconnect:        false,
    MaxReconnectInterval: 5,
    PersistentSession:    false,
    KeepAlive:            15,
    TLSCA:                "",
    TLSCert:              "",
    TLSKey:               "",
    Version:              mqtt.V3, // use mqtt.V5 for MQTTv5 client.
}
  • Then, create a connection to MQTT broker(s).
client, err := config.CreateConnection()
if err != nil {
    log.Fatal(err)
}
  • To disconnect from broker(s)
client.Disconnect()
  • To publish a message to broker(s) with a specific topic
err = client.Publish("topic", "message")
if err != nil {
    log.Fatal(err)
}
  • To handle new messages
client.Handle(func(topic string, payload []byte) {
    log.Printf("Received on [%s]: '%s'", topic, string(payload))
})

Check examples for more information about MQTTv5 client.

Documentation

Overview

Package mqtt provides easy-to-use MQTT connection for projects.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Brokers              []string      // MQTT Broker address. Format: scheme://host:port
	ClientID             string        // Client ID
	Username             string        // Username to connect the broker(s)
	Password             string        // Password to connect the broker(s)
	Topics               []string      // Topics for subscription
	QoS                  int           // QoS
	Retained             bool          // Retain Message
	AutoReconnect        bool          // Reconnect if connection is lost
	MaxReconnectInterval time.Duration // Maximum time that will be waited between reconnection attempts
	PersistentSession    bool          // Set persistent(clean start for v5) of session
	KeepAlive            uint16        // Keep Alive time in sec
	TLSCA                string        // CA file path
	TLSCert              string        // Cert file path
	TLSKey               string        // Key file path
	Version              Version       // MQTT Version of client
}

Config contains configurable options for connecting to broker(s).

func (*Config) CreateConnection

func (m *Config) CreateConnection() (MQTT, error)

CreateConnection will automatically create connection to broker(s) with MQTTConfig parameters.

type ConnectionState

type ConnectionState int

ConnectionState of the Client

const (
	// Disconnected : no connection to broker
	Disconnected ConnectionState = iota
	// Connected : connection established to broker
	Connected
)

type MQTT

type MQTT interface {
	// Handle handles new messages to subscribed topics.
	Handle(handler)
	// Publish sends a message to broker with a specific topic.
	Publish(string, interface{}) error
	// Request sends a message to broker and waits for the response.
	Request(string, interface{}, time.Duration, handler) error
	// RequestWith sends a message to broker with specific response topic,
	// and waits for the response.
	RequestWith(string, string, interface{}, time.Duration, handler) error
	// SubscribeResponse creates new subscription for response topic.
	SubscribeResponse(string) error
	// Respond sends message to response topic with correlation id (use inside HandleRequest).
	Respond(string, interface{}, []byte) error
	// HandleRequest handles imcoming request.
	HandleRequest(responseHandler)
	// GetConnectionStatus returns the connection status: Connected or Disconnected
	GetConnectionStatus() ConnectionState
	// Disconnect will close the connection to broker.
	Disconnect()
}

MQTT is an interface for mqttv3 and mqttv5 structs.

type Version

type Version int

Version of the client

const (
	// V3 is MQTT Version 3
	V3 Version = iota
	// V5 is MQTT Version 5
	V5
)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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