mqttc

package module
v0.0.0-...-225c279 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 14 Imported by: 0

README

MQTTC: MQTT v3.1.1 Client library

API

You can find very good documentation of the API in this link:

https://pkg.go.dev/github.com/otaxhu/mqttc

Purpose

I've created this package because all of the MQTT client packages weren't abstracting enough the API or it was not very Go idiomatic, with things such as "Asynchronous operations", concept inexistent in the language.

This package aims to be synchronous and idiomatic, with the possibility of spawning Goroutines if you want to run any of the functions provided in a different routine while doing other tasks.

References:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(fallbackClientId string, opts *ClientOptions) *Client

func (*Client) Publish

func (c *Client) Publish(topic string, qos uint8, msg io.Reader) error

func (*Client) Start

func (c *Client) Start(uri string, cleanSession bool) error

func (*Client) Stop

func (c *Client) Stop() error

func (*Client) Subscribe

func (c *Client) Subscribe(subscriber Subscriber, subscriptions ...Subscription)

func (*Client) SubscribeFunc

func (c *Client) SubscribeFunc(handler SubscriberFunc, subscriptions ...Subscription)

type ClientOptions

type ClientOptions struct {
	Will    *Will
	Session SessionManager
}

type MessageInfo

type MessageInfo struct {
	Payload   io.ReadCloser
	Topic     string
	Qos       uint8
	Retain    bool
	Duplicate bool
}

type SessionManager

type SessionManager interface {
	// Gets the client id from the previous session, if there is no client id from the previous
	// session, then GetClientId must return empty string and nil error. The operation must be
	// atomic and idempotent.
	GetClientId() (string, error)

	// Sets the client id, overriding the previous client id saved. The operation must be atomic.
	SetClientId(string) error

	// Cleans the previous session, cleaning the underlying packet id table and the queue of
	// messages from the session. The operation must be atomic and idempotent.
	//
	// IMPORTANT:
	//
	// Implementators must have a transaction mechanism in which if some of the deletions fails,
	// the session should recover to its previous state as if this method had not been called.
	CleanSession() error

	// Returns a packet id that has not been used by any other packet.
	//
	// It's required not to mark it as used in the packet id table until SaveMessage is called.
	// The operation must be atomic.
	NewPacketId() (uint16, error)

	// Gets the following message in the previous session message queue.
	//
	// If after calling NextMessage, DiscardMessageById with id set to the message id is not
	// called, then calls to NextMessage should return the same SessionMessage until
	// DiscardMessageById is called. The operation must be atomic.
	//
	// Implementators should return nil message and nil error if there is no more messages in the
	// queue.
	NextMessage() (*SessionMessage, error)

	// Gets message by its id in the message table. The operation must be atomic.
	//
	// Implementators should return nil message and nil error if there is no message with such id.
	GetMessageById(uint16) (*SessionMessage, error)

	DiscardMessageById(uint16) error

	// Saves the message to the session manager. The operation must be atomic.
	//
	// IMPORTANT:
	//
	// Implementators must mark the packet id from this message as used in the packet id table if
	// it haven't been marked yet.
	SaveMessage(message SessionMessage) error
}

Session manager that saves the state of the MQTT session, must be implemented if you want to subscribe or publish to topics with QoS > 0.

Can be implemented with SQL or NoSQL databases like MySQL or MongoDB.

It's discouraged to implement it with volatile environments such as temporal files or Go data structures.

type SessionMessage

type SessionMessage struct {
	// Required in all of the instances of SessionMessage
	PacketId uint16
	Type     SessionMessageType

	// Required only if Type is SessionMessage_PUBLISH
	Qos uint8
	// Payload should be nil if ToSend is false because this field will be ignored
	Payload []byte
	// Retain will be ignored if ToSend is false
	Retain bool

	// ToSend distinguishes between a packet to be send or a packet received
	//
	// true: A packet that will be send with DUP flag true if Type is PUBLISH
	//
	// false: A packet that was received as it is. A corresponding packet will be sent as response
	ToSend bool
}

Represent a message to be saved to the SessionManager.

SessionMessage's returned by SessionManager are assumed to be duplicates when are sended to server.

type SessionMessageType

type SessionMessageType int
const (
	SessionMessage_PUBLISH SessionMessageType = 3
	SessionMessage_PUBACK  SessionMessageType = 4
	SessionMessage_PUBREC  SessionMessageType = 5
	SessionMessage_PUBREL  SessionMessageType = 6
	SessionMessage_PUBCOMP SessionMessageType = 7
)

type Subscriber

type Subscriber interface {
	ReceiveMessage(message MessageInfo)
}

type SubscriberFunc

type SubscriberFunc func(MessageInfo)

func (SubscriberFunc) ReceiveMessage

func (f SubscriberFunc) ReceiveMessage(message MessageInfo)

type Subscription

type Subscription struct {
	Topic string
	Qos   uint8
}

type Will

type Will struct {
	Message string
	Topic   string
	Qos     uint8
	Retain  bool
}

Jump to

Keyboard shortcuts

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