1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-27 13:48:49 +08:00
__touk__ f8ce94e9bb
NOISSUE - Refactor MQTT subscriber (#1561)
* correct suscriber interface validator + refactore token error handling

Signed-off-by: tzzed <zerouali.t@gmail.com>

* apply review suggestion

Signed-off-by: tzzed <zerouali.t@gmail.com>

Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
2022-02-09 10:19:09 +01:00

42 lines
721 B
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package mqtt
import (
"errors"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
const (
protocol = "mqtt"
username = "mainflux-mqtt"
qos = 2
)
var errConnect = errors.New("failed to connect to MQTT broker")
func newClient(address string, timeout time.Duration) (mqtt.Client, error) {
opts := mqtt.NewClientOptions().
SetUsername(username).
AddBroker(address)
client := mqtt.NewClient(opts)
token := client.Connect()
if token.Error() != nil {
return nil, token.Error()
}
ok := token.WaitTimeout(timeout)
if !ok {
return nil, errConnect
}
if token.Error() != nil {
return nil, token.Error()
}
return client, nil
}