Client Objects-Working with The Python MQTT Client

client-objectsThe main component of the Paho Python MQTT client is the client class.

Before you can publish or subscribe to MQTT messages you will need to create a new client object from the client class.

The client constructor takes 4 optional parameters. The default values are shown below:

Client(client_id=””, clean_session=True, userdata=None, protocol=MQTTv311, transport=”tcp”)

To create a new client object you first need to import the Paho MQTT client, and then create the object as shown below:

import paho.mqtt.client as mqtt
client1 = mqtt.Client()

The Client name

Although the client name is optional, it is only optional if clean sessions. are True (default).

However even if you don’t provide a client name one is created for you by the client software.

The screen shot below is taken from the MQTT broker console and shows the result of the client connecting first without specifying a client id and then secondly supplying a client id.

In both case the broker sees a client id as the client will auto generate a random one.

mqtt-client-id
The client name is used by the MQTT broker to identify the client.

This is necessary when using persistent connections as the broker needs to store messages for the client when the client isn’t connected.

Duplicate Client ids

If a client connects with a client id that is in use, and also currently connected then the existing connection is closed.

Because the MQTT client may attempt to reconnect following a disconnect this can result in a loop of disconnect and connect.

The screen shots below show what happens when I try and connect a client (client2) to the broker using the same id as an existing client (client1).

mqtt-duplicate-client-id

Here is the view from the broker:

mqtt-broker-duplicate-ids

In the above you can clearly see that when client 2 connects client 1 gets disconnected. Client 1 then attempts a reconnect which in turn disconnects client 2.

Therefore be careful when assigning client IDs.

Clean Session Flag

This flag tells the broker to either

  • Remember subscriptions and Store messages that the client has missed because it was offline value =False or
  • Not to Remember subscriptions and not to Store messages that the client has missed because it was offline – value =True

By default it is set to True

See persistent connections by example to see how persistent connections and clean sessions work.

Other Parameters

The user data and protocol parameters are discussed here and I wont discuss them here as the defaults are normally used.

Auxiliary Functions or Settings

There are several Client settings that may need to be changed before a connection is created.

These settings are changed using auxiliary functions. Here is a list of the functions that are available:

  • max_inflight_messages_set()Affects Message throughput
  • max_queued_messages_set()Affects Message throughput
  • message_retry_set(retry)Affects Message throughput
  • tls_set()Used for SSL security
  • tls_insecure_set(value)Used for SSL security
  • username_pw_set()  – Used for username and passwords
  • will_set()Used for setting last will and testament

The most common used ones are username_pw_set() and will_set().

See the Documentation for details

Websockets

websocket support is also built into the Paho MQTT client.

To Use Websockets with Python. Create the client object using the transport=websockets argument.

client1= paho.Client("control1",transport='websockets')

See Introduction to MQTT over WebSockets

Simple Client Object Modifications I Make

I usually add additional flags to the Client object and use then for detecting successful connections and subscribes.

This I do before I create the client object. So often in my test scripts you will see an initialise client object function that looks like this.


def Initialise_client_object():
#flags set
mqtt.Client.bad_connection_flag=False
mqtt.Client.connected_flag=False
mqtt.Client.disconnected_flag=False
mqtt.Client.suback_flag=False

Common Questions and Answers

Q- Can two clients have the same client ID?

A- No. If a client connects with a client id that is also currently connected then the existing connection is closed.

Next=—> Client Connections

Using The Paho Python MQTT Client Tutorials

Related tutorials and Resources

Please rate? And use Comments to let me know more
[Total: 2    Average: 4.5/5]