Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/amenzhinsky/mqtt
Micro MQTT library for Golang
https://github.com/amenzhinsky/mqtt
Last synced: 2 days ago
JSON representation
Micro MQTT library for Golang
- Host: GitHub
- URL: https://github.com/amenzhinsky/mqtt
- Owner: amenzhinsky
- Created: 2019-08-23T11:07:29.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2019-12-10T07:17:49.000Z (almost 5 years ago)
- Last Synced: 2024-06-20T13:49:26.630Z (5 months ago)
- Language: Go
- Size: 53.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mqtt
Micro [MQTT 3.1.1](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html) client for [Golang](https://golang.org/).
It provides low-level API and doesn't support async usage and advanced features such as reconnects, fallback addresses and persistent storage.
## Usage
```go
conn, err := net.Dial("tcp", "127.0.0.1:8883")
if err != nil {
return err
}client := mqtt.New(conn, mqtt.WithMessagesHandler(func(pk *packet.Publish) {
switch {
case mqtt.Match("/dev/cmd/speed", pk.Topic):
speed(binary.BigEndian.Uint32(pk.Payload))
case mqtt.Match("/dev/cmd/+/play", pk.Topic):
play(strings.Split(pk.Topic, "/")[3], pk.Payload)
}
}))
defer client.Close()if _, err := client.Connect(context.Background(),
packet.WithConnectCleanSession(true),
packet.WithConnectWill("/dev/online", []byte{0}, packet.QoS1, true),
); err != nil {
return err
}if _, err := client.Subscribe(context.Background(),
packet.WithSubscribeTopic("/dev/cmd/#", packet.QoS1),
); err != nil {
return err
}if err := client.Publish(context.Background(),
"/dev/online",
packet.WithPublishQoS(packet.QoS1),
packet.WithPublishPayload([]byte{1}),
); err != nil {
return err
}if err := client.Disconnect(context.Background()); err != nil {
return err
}
```