Marco Stramezzi
Marco Stramezzi

Reputation: 2281

MQTT broker connection management

I'm using Paho to communicate with an MQTT broker and all the example I found (like this) do these 3 steps when performing an action (publish or subscribe):

  1. connect to the broker
  2. do action
  3. disconnect

My question is: are there any drawbacks holding a connection for the whole life of the application instead of opening/closing it for each action? Isn't it a faster solution removing the time for opening the connection?

Upvotes: 0

Views: 852

Answers (1)

hardillb
hardillb

Reputation: 59816

No, holding a connection open for the lifetime of the application is a fully expected usecase, it's the only real way you'd be able to subscribe to a topic and receive messages when they are published.

The protocol has built in ping messages to ensure the broker knows the client is still connected.

The examples tend to be relatively trivial but want to show the full life cycle of the client which is why they connect, do something, disconnect

Upvotes: 2

Related Questions