shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
552 stars 179 forks source link

Disconnect exactly every minute and also When publishing message #47

Closed tje3d closed 5 years ago

tje3d commented 5 years ago

Here is my connection code in flutter (mqtt_client: ^3.3.4):

Code:

...
_client = MqttClient(
  "wss://${mqttServer}/mqtt",
  clientId,
);
_client.port = 443;
_client.useWebSocket = true;
...
_client.connectionMessage = MqttConnectMessage()
  .withWillQos(MqttQos.exactlyOnce)
  .authenticateAs(id.toString(), token)
  .withClientIdentifier(clientId);
...
await _client.connect();

The problem is, Sometimes without any reason, i get disconnected from server also when trying to publish a message.

MqttClientPayloadBuilder builder = MqttClientPayloadBuilder();
builder.addString(
  json.encode(
    {
      "type": "msgText",
      "data": msg.data,
      "identifier": identifier,
    },
  ),
);
_client.publishMessage(topic, MqttQos.exactlyOnce, builder.payload)

I'm sure this is a library issue cause i have no problem with paho.mqtt.

Edit:

It's disconnect exactly every minute.

I/flutter (23865): 2018-11-19 11:39:30.386998 -- MqttConnection::_onDone - calling disconnected callback
shamblett commented 5 years ago

Hmm, this sounds like the default keepalive period(60s), try setting this to a lower value, i.e.

client.keepAlivePeriod = 20;

tje3d commented 5 years ago

@shamblett yes that's it. But it seems the behavior is wrong.

shamblett commented 5 years ago

Ok, in what sense, do you mean the behaviour is wrong? 60 seconds seems a fairly common value, some of this depends on your broker of course. A value of 0 disables keep alive, is this what you would prefer as a default?

tje3d commented 5 years ago

Ahh i was wrong, i thought that connection should be always up and Automatically reconnect when disconnected, as i seen in Paho.

It seems this package works properly (http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349238).

But I think it should disconnect client only if it is idle for keepAlive time. (not sure if receiving packet count as activation).

Anyway thanks for your perfect job.

shamblett commented 5 years ago

OK, no probs, closing