Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lucacasonato/mqtt
A nice MQTT client library that wraps paho.mqtt.golang
https://github.com/lucacasonato/mqtt
golang internet-of-things mqtt
Last synced: 2 months ago
JSON representation
A nice MQTT client library that wraps paho.mqtt.golang
- Host: GitHub
- URL: https://github.com/lucacasonato/mqtt
- Owner: lucacasonato
- License: mit
- Created: 2019-10-19T21:11:33.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-02-24T23:23:22.000Z (almost 2 years ago)
- Last Synced: 2024-10-06T10:50:26.308Z (3 months ago)
- Topics: golang, internet-of-things, mqtt
- Language: Go
- Homepage: https://godoc.org/github.com/lucacasonato/mqtt
- Size: 43 KB
- Stars: 19
- Watchers: 4
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mqtt
[![GoDoc](https://godoc.org/github.com/lucacasonato/mqtt?status.svg)](http://godoc.org/github.com/lucacasonato/mqtt)
[![CI](https://github.com/lucacasonato/mqtt/workflows/ci/badge.svg)](https://github.com/lucacasonato/mqtt/actions?workflow=ci)
[![Code Coverage](https://img.shields.io/codecov/c/gh/lucacasonato/mqtt)](https://codecov.io/gh/lucacasonato/mqtt)
[![Go Report](https://goreportcard.com/badge/github.com/lucacasonato/mqtt)](https://goreportcard.com/report/github.com/lucacasonato/mqtt)An mqtt client for Go that improves usability over the [paho.mqtt.golang](https://github.com/eclipse/paho.mqtt.golang) library it wraps. Made for 🧑.
## installation
```bash
go get github.com/lucacasonato/mqtt
``````go
import "github.com/lucacasonato/mqtt"
// or
import (
"github.com/lucacasonato/mqtt"
)
```## usage
### creating a client & connecting
```go
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
```go
client.Disconnect()
```### publishing a message
#### bytes
```go
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
```go
err := client.PublishString(context.WithTimeout(1 * time.Second), "api/v0/main/client1", "hello world", mqtt.AtLeastOnce)
if err != nil {
panic(err)
}
```#### json
```go
err := client.PublishJSON(context.WithTimeout(1 * time.Second), "api/v0/main/client1", []string("hello", "world"), mqtt.AtLeastOnce)
if err != nil {
panic(err)
}
```### subscribing
```go
err := client.Subscribe(context.WithTimeout(1 * time.Second), "api/v0/main/client1", mqtt.AtLeastOnce)
if err != nil {
panic(err)
}
``````go
err := client.SubscribeMultiple(context.WithTimeout(1 * time.Second), map[string]mqtt.QOS{
"api/v0/main/client1": mqtt.AtLeastOnce,
})
if err != nil {
panic(err)
}
```### handling
```go
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
```go
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()
```