MQTT Connections – Should You Stay Connected

MQTT is a connection oriented protocol which means that you need to establish  a connection before you can send data.

However what happens after the data is sent? Do you disconnect? or stay connected?

To answer the question you must understand the data that is being sent.In particular you must consider.

  • Is data sent at regular intervals and how often
  • Is data sent spontaneously in response to an event.
  • Is the data needed in real time
  • How long does it take to send the data.
  • Number of Clients
  • Network Connection Type.

MQTT Connection Times

How long it takes to establish an MQTT connection depends on many factors like network distance,network load,server load.

On a local network a connection time of around 1 second is quite normal.

A connection over the Internet or WAN however can take several seconds and can vary a lot.

Is Data Sent at Regular Intervals and How Often

The more frequently the data is sent then the more likely a connection should be retained and not disconnected.

So if we take a connection time of approx 1 second and data is sent every 5 seconds then the connection should be maintained.

If however data is sent every hour then dropping the connection after sending the data would be the most logical thing to do.

Is Data Sent Spontaneously in Response to an Event

Imagine a fire alarm sensor which only sends data if there is an alarm. If we take a connection time of 1 second then is it reasonable to have the sensor connect and send data and disconnect or should it keep a permanent connection.

How long a delay is tolerable between the event being triggered and the notification being registered.

For a fire alarm 1 second would probably be OK but 5 seconds too long

How Long Does it Take to Send the Data

If you are sending large amounts of data e.g a file, movie,Images, blockchain then the data transfer could last many seconds.

If data is being sent at regular intervals e,g every 5 minutes and takes several minutes to send then it would be sensible to keep the connection open.

However if data is sent every hour and lasts 5 minutes then you can safely close the connection after sending.

Number of Clients

The number of clients has a great impact on the broker load and the network load.

If the connection is held open then each client will generate keepalive traffic.

It would be possible to minimise this by increasing the keepalive interval but doing this would mean that failed connections would be missed.

Network Connection Type

Devices connected over a low bandwidth connection or a costly connection will need to limit their connection time.

For example, if you are connecting over a mobile network and paying per minute then you don’t want to hold the connection open.

Likewise if multiple clients are connected using a low bandwidth  connection then not using it for keep alive traffic would be sensible.


Pros and Cons of Holding a Connection Open



no set up time delay Set up time delay
connection is present and can be used immediately May not be possible to establish a connection
Uses resources on broker even when idle
Uses network resources  even when idle (keep alive)
Uses client resources  even when idle (keep alive). Client cannot sleep.
Works with all data transmission intervals
Works with event type data
Works in both directions i.e commands can be sent to the sensor.


There are many factors to consider when deciding whether or not to hold a client connection open on a permanent basis.

In general I favour closing the connection if possible.


Do you have real world experience with this topic? If so I would be grateful for your feedback. Please use the comments form below or if you prefer use the contact page.

Related tutorials and Resources:

Please rate? And use Comments to let me know more


  1. You don’t mention power? If a client is battery powered and spending most of it’s time in deep-sleep then disconnects are inevitable.
    Although I’m fairly new to MQTT (and Node Red), with limited experience, I agree with your assessments, and if power isn’t a problem I just use periodic or event-triggered updates. It hadn’t even occurred to me to disconnect (with plenty of power), though my stuff is still small scale.
    It’s great to have this sort of info available and it’s helped me, so thank you for your continued efforts.

Leave a Reply

Your email address will not be published.