pascaldekloe / mqtt

Message Queuing Telemetry Transport Client 🤖
Other
41 stars 4 forks source link

Keep alive mechanism not implemented? #5

Closed brutella closed 1 year ago

brutella commented 1 year ago

Does this library send any ping requests to a broker after the keep alive timeout?

The mqttc client sets the keep alive timeout to 0 by default. When I set it to 60 seconds, my connection gets closed by the broker because no control packet was sent within 90 seconds after a connect. (see MQTT v3.1.1 [3.1.2.10 Keep Alive])

pascaldekloe commented 1 year ago

The keep-alive setting does not trigger any special behaviour in the client. Users could apply the timeout as a health check. Think of worker 🤖 which are supposed to do things on a regular basis. MQTT keep-alive can verify, log and boot stale 🤖 all from the server side. A 🤖 may PING the server when it deliberately choses to skip a shift.

What do you want to achieve with the keep-alive setting @brutella? If it's the connection you worry about, then perhaps utilize TCP instead?

func NewKeepAliveDialer(addr string) mqtt.Dialer {
    return func(ctx context.Context) (net.Conn, error) {
        dialer := net.Dialer{ KeepAlive: time.Minute }
        return dialer.DialContext(ctx, "tcp", address)
    }
}
brutella commented 1 year ago

What do you want to achieve with the keep-alive setting @brutella?

When I specify a keep alive timeout of 60 seconds, I get disconnected from the broker after 90 seconds. I thought that this client implementation automatically handles the case, where when no control packet was sent within 60 seconds, it automatically sends a ping message to the broker to keep the connection alive.

pascaldekloe commented 1 year ago

I can make the client do that. Not sure to what purpose though. If you have a reason why you'd want to ping the server then please share and we can make something happen perhaps.