eclipse-paho / paho.mqtt.golang

Other
2.77k stars 534 forks source link

can't connect: identifier rejected if SubscribeMultiple(filters map[string]byte, callback MessageHandler) Token is used #626

Closed sohneg closed 1 year ago

sohneg commented 1 year ago

I try to subscribe to multiple topics. But it won't work.

What could cause this error? If i change it back to the single Subscribe it works.

Client options:

var broker = scnfg.MqttOpt.Broker
    var port = scnfg.MqttOpt.Port
    opts := mqtt.NewClientOptions()
    opts.AddBroker(fmt.Sprintf("tcp://%s:%s", broker, port))
    opts.SetClientID(scnfg.MqttOpt.ClientId)
    opts.SetKeepAlive(60)
    opts.SetAutoReconnect(true)
    opts.SetDefaultPublishHandler(messagePubHandler)
    opts.OnConnect = connectHandler
    opts.OnConnectionLost = connectLostHandler
    client := mqtt.NewClient(opts)

How i use it:

func subcribe(client mqtt.Client) {
    topicMap := make(map[string]byte)
    for _, topic := range scnfg.MqttOpt.Topics {
        topicMap[topic] = 0
        log.Printf("Subscribed to topic: %s", topic)
    }
    token := client.SubscribeMultiple(topicMap, nil)
    token.Wait()
}

What Mosquitto says:

1672921173: New connection from ::1:58879 on port 1883.
1672921173: New client connected from ::1:58879 as TAM1 (p2, c1, k0).
1672921173: No will message specified.
1672921173: Sending CONNACK to TAM1 (0, 2)
1672921173: Bad socket read/write on client TAM1: Invalid arguments provided.
1672921173: New connection from ::1:58880 on port 1883.
1672921173: New client connected from ::1:58880 as TAM1 (p1, c1, k0).
1672921173: No will message specified.
1672921173: Sending CONNACK to TAM1 (0, 2)
1672921173: Bad socket read/write on client TAM1: Invalid arguments provided.
sohneg commented 1 year ago

FIX: I removed opts.SetKeepAlive(60) and it worked

tomatod commented 1 year ago

@sohneg Argument of SetKeepAlive is time.Second instead of int. https://pkg.go.dev/github.com/eclipse/paho.mqtt.golang#ClientOptions.SetKeepAlive

So, you can write opts.SetKeepAlive(60 * time.Second)

MattBrittan commented 1 year ago

As @tomatod says calling opts.SetKeepAlive(60) effectively sets keepalives to 0 seconds (because the option is a time.Duration and 60 nanoseconds will be rounded down to 0 seconds). Mosquitto (v2+) will not, by default, accept a keepalive of 0 (which means no keepalives) so it refuses the connection.

I'm surprised that your code would work with a single subscription...

MattBrittan commented 1 year ago

Closing as this behaviour is expected when Keepalive is 0 (well not quite as the OP wrote but I can't duplicate the behaviour originally reported).