Categorygithub.com/creedscode/mqtt
modulepackage
0.2.1
Repository: https://github.com/creedscode/mqtt.git
Documentation: pkg.go.dev

# README

mqtt

!!!! THIS REPO IS NOT THE OG - CHECK THE ORIGINAL THIS IS A FORK THAT WILL BE MADE COMPATIBLE WITH THE LATEST PAHO VERSION. !!!! GoDoc CI Code Coverage Go Report

A fork of an mqtt client for Go that improves usability over the paho.mqtt.golang library it wraps. Made for 🧑. Check out the OG: https://github.com/lucacasonato/mqtt

installation

go get github.com/creedscode/mqtt
import "github.com/creedscode/mqtt"
// or
import (
    "github.com/creedscode/mqtt"
)

usage

creating a client & connecting

client, err := mqtt.NewClient(mqtt.ClientOptions{
    // required
    Servers: []string{
        "tcp://test.mosquitto.org:1883",
    },

    // optional
    ClientID: "my-mqtt-client",
    Username: "admin",
    Password: "***",
    AutoReconnect: true,
})
if err != nil {
    panic(err)
}

err = client.Connect(context.WithTimeout(2 * time.Second))
if err != nil {
    panic(err)
}

You can use any of these schemes for the broker tcp (unesecured), ssl (secured), ws (unsecured), wss (secured).

disconnecting from a client

client.Disconnect()

publishing a message

bytes

err := client.Publish(context.WithTimeout(1 * time.Second), "api/v0/main/client1", []byte(0, 1 ,2, 3), mqtt.AtLeastOnce)
if err != nil {
    panic(err)
}

string

err := client.PublishString(context.WithTimeout(1 * time.Second), "api/v0/main/client1", "hello world", mqtt.AtLeastOnce)
if err != nil {
    panic(err)
}

json

err := client.PublishJSON(context.WithTimeout(1 * time.Second), "api/v0/main/client1", []string("hello", "world"), mqtt.AtLeastOnce)
if err != nil {
    panic(err)
}

subscribing

err := client.Subscribe(context.WithTimeout(1 * time.Second), "api/v0/main/client1", mqtt.AtLeastOnce)
if err != nil {
    panic(err)
}
err := client.SubscribeMultiple(context.WithTimeout(1 * time.Second), map[string]mqtt.QOS{
    "api/v0/main/client1": mqtt.AtLeastOnce,
})
if err != nil {
    panic(err)
}

handling

route := client.Handle("api/v0/main/client1", func(message mqtt.Message) {
    v := interface{}{}
    err := message.PayloadJSON(&v)
    if err != nil {
        panic(err)
    }
    fmt.Printf("recieved a message with content %v\n", v)
})
// once you are done with the route you can stop handling it
route.Stop()

listening

messages, route := client.Listen("api/v0/main/client1")
for {
    message := <-messages
    fmt.Printf("recieved a message with content %v\n", message.PayloadString())
}
// once you are done with the route you can stop handling it
route.Stop()

# Packages

No description provided by the author

# Functions

NewClient creates a new client with the specified options.

# Constants

AtLeastOnce means the broker will deliver a message at least once to every subscriber.
AtMostOnce means the broker will deliver at most once to every subscriber - this means message delivery is not guaranteed.
ExactlyOnce means the broker will deliver a message exactly once to every subscriber.
Retain tells the broker to retain a message and send it as the first message to new subscribers.

# Variables

ErrMinimumOneServer means that at least one server should be specified in the client options.

# Structs

Client for talking using mqtt.
ClientOptions is the list of options used to create a client.
A Message from or to the broker.
Route is a receipt for listening or handling certain topic.

# Type aliases

A MessageHandler to handle incoming messages.
PublishOption are extra options when publishing a message.
QOS describes the quality of service of an mqtt publish.