mqtt

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2019 License: MIT Imports: 7 Imported by: 1

README

mqtt

GoDoc CI Code Coverage Go Report Card

An mqtt client for Go that improves usability over the paho.mqtt.golang library it wraps. Made for 🧑.

installation

go get github.com/lucacasonato/mqtt
import "github.com/lucacasonato/mqtt"
// or
import (
    "github.com/lucacasonato/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)
}
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()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMinimumOneServer = errors.New("mqtt: at least one server needs to be specified")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	Options ClientOptions // The options that were used to create this client
	// contains filtered or unexported fields
}

Client for talking using mqtt

func NewClient

func NewClient(options ClientOptions) (*Client, error)

NewClient creates a new client with the specified options

func (*Client) Connect

func (c *Client) Connect(ctx context.Context) error

Connect tries to establish a conenction with the mqtt servers

func (*Client) DisconnectImmediately

func (c *Client) DisconnectImmediately()

Disconnect will immediately close the conenction with the mqtt servers

func (*Client) Handle

func (c *Client) Handle(topic string, handler MessageHandler) Route

func (*Client) Listen

func (c *Client) Listen(topic string) (chan Message, Route)

func (*Client) Publish

func (c *Client) Publish(ctx context.Context, topic string, payload []byte, qos QOS, options ...PublishOption) error

func (*Client) PublishJSON

func (c *Client) PublishJSON(ctx context.Context, topic string, payload interface{}, qos QOS, options ...PublishOption) error

func (*Client) PublishString

func (c *Client) PublishString(ctx context.Context, topic string, payload string, qos QOS, options ...PublishOption) error

func (*Client) Subscribe

func (c *Client) Subscribe(ctx context.Context, topic string, qos QOS) error

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(ctx context.Context, topic string) error

type ClientOptions

type ClientOptions struct {
	Servers  []string // The list of broker hostnames to connect to
	ClientID string   // If left empty a uuid will automatically be generated
	Username string   // If not set then authentication will not be used
	Password string   // Will only be used if the username is set

	AutoReconnect bool // If the client should automatically try to reconnect when the connection is lost
}

ClientOptions is the list of options used to create a client

type Message

type Message struct {
	// contains filtered or unexported fields
}

func (*Message) Acknowledge

func (m *Message) Acknowledge()

func (*Message) IsDuplicate

func (m *Message) IsDuplicate() bool

func (*Message) Payload

func (m *Message) Payload() []byte

func (*Message) PayloadJSON

func (m *Message) PayloadJSON(v interface{}) error

func (*Message) PayloadString

func (m *Message) PayloadString() string

func (*Message) QOS

func (m *Message) QOS() QOS

func (*Message) Topic

func (m *Message) Topic() string

func (*Message) TopicVars

func (m *Message) TopicVars() []string

type MessageHandler

type MessageHandler func(Message)

type PublishOption

type PublishOption int
const (
	Retain PublishOption = iota
)

type QOS

type QOS byte

QOS describes the quality of service of an mqtt publish

const (
	AtMostOnce  QOS = iota // Deliver at most once to every subscriber - this means message delivery is not guaranteed
	AtLeastOnce            // Deliver a message at least once to every subscriber
	ExactlyOnce            // Deliver a message exactly once to every subscriber
)

type Route

type Route struct {
	// contains filtered or unexported fields
}

func (*Route) Stop

func (r *Route) Stop()

Stop removes this route from the router and stops matching it

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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