https://github.com/easygithdev/mqtt
Implement MQTT client
https://github.com/easygithdev/mqtt
Last synced: 2 months ago
JSON representation
Implement MQTT client
- Host: GitHub
- URL: https://github.com/easygithdev/mqtt
- Owner: EasyGithDev
- License: mit
- Created: 2022-03-24T07:58:40.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-04-18T13:46:59.000Z (about 3 years ago)
- Last Synced: 2025-01-29T23:30:15.852Z (4 months ago)
- Language: Go
- Size: 177 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Mqtt client
I'll try to implement the draft as mentionned here :https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/
## Use as a binary
Open a terminal and write :
```bash
./make
```Or you can edit the make file to perfome your own buid command.
You will find the binary in the bin folder.
You can send a publish like above :
```bash
./bin/client -pub -h=test.mosquitto.org -t=hello/mqtt "-m=hello world"
```You can subscribe to a topic and read the messages like above :
```bash
./bin/client -sub -h=test.mosquitto.org -t=hello/mqtt
```## Use as a library
#### Connection
Simple connect with :
- clientId
- hostname
- port```go
clientId := "test-golang-mqtt"
connHost := "test.mosquitto.org"
connPort := "1883"mc := client.New(
// client Id
clientId,
// connection infos
client.WithConnInfos(conn.New(connHost, conn.WithPort(connPort))),
)_, connErr := mc.Connect()
if connErr != nil {
log.Print("Error connecting:", connErr.Error())
}
defer mc.Close()
```Connect with credentials :
- Username
- Password```go
...
username := "rw"
password := "readwrite"
mc := client.New(
// client Id
clientId,
// Credentials
client.WithCredentials(username, password),
// connection infos
client.WithConnInfos(conn.New(connHost, conn.WithPort(connPort))),
)...
```#### Publish
Publish a message :
- topic : the topic that the message should be published on.
- message : the actual message to send.
- qos : the quality of service level to use.
- retain : if set to True, the message will be set as the “last known good”/retained message for the topic.```go
topic := "hello/mqtt"
qos := client.QOS_0
msg := "The temperature is 5 degrees"_, pubErr := mc.Publish(topic, msg, byte(qos))
if pubErr != nil {
log.Print("Error publishing:", pubErr.Error())
}
```Publish many messages :
```go
go mc.LoopStart()
for {
temperature := rand.Intn(60)
msg := "The temperature is " + fmt.Sprintf("%d", temperature)
_, pubErr := mc.Publish(topic, msg, byte(qos))if pubErr != nil {
log.Print("Error publishing:", pubErr.Error())
break
}
time.Sleep(5 * time.Second)
}```
#### Subscribe
Subscribe with :
- topic : a string specifying the subscription topic to subscribe to.
- qos : the desired quality of service level for the subscription.```go
topic := "hello/mqtt"
qos := client.QOS_0_, errSub := mc.Subscribe(topic, client.QOS_0)
if errSub != nil {
log.Printf("Subscribe Error: %s\n", errSub)
}
```Get the messages :
```go
_, errSub := mc.Subscribe(topic, client.QOS_0)
if errSub != nil {
log.Printf("Subscribe Error: %s\n", errSub)
} else {
mc.LoopForever()
}
```#### Callback function
```go
var onConnect = func(mc client.MqttClient, userData interface{}, rc net.Conn) {
fmt.Println("Connecting to server " + rc.RemoteAddr().String())
}var onDisconnect = func(mc client.MqttClient, userData interface{}, rc net.Conn) {
fmt.Println("Disconnect from server" + rc.RemoteAddr().String())
}var onPublish = func(mc client.MqttClient, userData interface{}, mid int) {
fmt.Printf("Publish\n")
}var onSubscribe = func(mc client.MqttClient, userData interface{}, mid int) {
fmt.Printf("Subscribe\n")
}var onMessage = func(mc client.MqttClient, userData interface{}, message string) {
fmt.Println("msg: " + message)
}...
mc.OnConnect = onConnect
mc.OnDisconnect = onDisconnect
mc.OnPublish = onPublish
mc.OnSubscribe = onSubscribe
mc.OnMessage = onMessage
```