https://github.com/lynnplus/go-mqtt
mqtt client for golang
https://github.com/lynnplus/go-mqtt
mqtt mqtt-client mqttv3 mqttv5
Last synced: 5 months ago
JSON representation
mqtt client for golang
- Host: GitHub
- URL: https://github.com/lynnplus/go-mqtt
- Owner: lynnplus
- License: apache-2.0
- Created: 2024-05-04T07:38:33.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-05-14T07:41:32.000Z (about 2 years ago)
- Last Synced: 2024-06-19T11:23:12.361Z (almost 2 years ago)
- Topics: mqtt, mqtt-client, mqttv3, mqttv5
- Language: Go
- Homepage:
- Size: 155 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-mqtt
[](https://pkg.go.dev/github.com/lynnplus/go-mqtt)


[](https://goreportcard.com/report/github.com/lynnplus/go-mqtt)
[](https://github.com/lynnplus/go-mqtt/blob/master/LICENSE)
go-mqtt is a mqtt golang client that implements the mqttv3 and mqttv5 protocols
## Done
- basic client
- packet reading and writing
- publish qos 0
- subscribe and unsubscribe
- re-connector
## TODO
- mqttv3 check
- qos 1 and 2
- prefix tree based message router
- event hook
## Links
[MQTT-v5.0 oasis doc](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html)
[MQTT-v3.1.1 oasis doc](https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html)
## How to use
get package:
```shell
go get github.com/lynnplus/go-mqtt
```
connect to mqtt broker
```go
package main
import (
"github.com/lynnplus/go-mqtt"
"github.com/lynnplus/go-mqtt/packets"
)
func main() {
client := mqtt.NewClient(&mqtt.ConnDialer{
Address: "tcp://127.0.0.1:1883",
}, mqtt.ClientConfig{})
pkt := packets.NewConnect("client_id", "username", []byte("password"))
ack, err := client.Connect(context.Background(), pkt)
if err != nil {
panic(err)
}
if ack.ReasonCode != 0 {
panic(packets.NewReasonCodeError(ack.ReasonCode, ""))
}
//do something
_ = client.Disconnect()
}
```
The package provides two connection methods,
synchronous and asynchronous. When asynchronous,
the response result can only be obtained in the callback.
Asynchronous call connection:
```go
package main
func main() {
client := mqtt.NewClient(&mqtt.ConnDialer{
Address: "tcp://127.0.0.1:1883",
}, mqtt.ClientConfig{
OnConnected: func(c *mqtt.Client, ack *packets.Connack) {
fmt.Println(ack.ReasonCode)
},
})
pkt := packets.NewConnect("client_id", "username", []byte("password"))
err := client.StartConnect(context.Background(), pkt)
}
```
For a basic chat example please see: https://github.com/lynnplus/go-mqtt/blob/master/examples/chat/main.go
## Features
### Dialer
The package provides a dialer that implements tcp and tls by default.
If the user needs other connection protocol support, such as websocket,
the Dialer interface provided in the package can be implemented.
### MQTTv5 Enhanced authentication
- scram : [example](https://github.com/lynnplus/go-mqtt/blob/master/auther.go)
### Re-connect
...
### Router
...