Publish and Subscribe Using the Python MQTT-SN Client

In this tutorial we will look at how you can subscribe and publish to topics using the Python MQTT-SN client.

In addition we also look at topic registration as this is also part of the publish process

The client is available to download at the bottom of this page.


The client uses the subscribe function to subscribe to topics. The subscribe function is a blocking function and waits for a subscribe acknowledge before returning.

If successful it returns a topic Id  and reason code. A reason code of 0 is a success.

The message ID is not required as the code waits for a subscribe acknowledgement.

The subscribe takes two arguments the topic and the QOS which is optional, and defaults to 0.

TopicId, rc=client.subscribe(topic,qos)

the topic can be:

  1. A long topic name as per MQTT
  2. A topic ID
  3. A short Topic name
  4. A predefined topic

Using topic name as per MQTT

As per MQTT 3.1 a topic name is a UTF8-encoded string that can include wild card characters + and #.

When you subscribe to a topic using a topic name the subscribe functions returns a topic ID for this topic name.

The topic ID can then be used to publish messages on this topic.

TopicId, rc=client.subscribe(“long_topic_name”)

Using a Topic ID

Although you can use a topic ID to subscribe it is not normally done as you must first find the topic id using the registration process.

So you need to use:

  1. register topic
  2. subscribe topic

Using a Short Topic Name

A short topic name is just  characters. Example using the short topic name AA as an example


The topic Id returned in this case is the short topic name.

Because the topic Id is required by the publish method then you will need to store a  table of topic names and topic IDs.

The following code snippet shows the code for the above three examples

print("subscribing to topics ")

print("using long topic name")
print("topic id =",TopicId1," reason code = ",rc)
print("registering topic")
print("subscribing to topic using topic Id=",TopicId2)
print("subscribed topic id =",TopicId," reason code = ",rc)

print("subscribe using short topic name AA")
print("subscribed topic id =",TopicId3," reason code = ",rc)


And the screen shot below shows the output.

Notice that subscribing with the short topic name returns the short topic name.

Using a Predefined Topic Name

Topics can be predefined on both the broker and client. Predefined topic names don’t require topic registration, but they do require a topic list.

For a client to subscribe to a predefined topic -e.g. topic1 then this topic must be defined on the broker in a predefined topic list.

Predefined topics aren’t currently supported on RSMB, but they are supported on the paho Gateway.

Topic Registration

A client can register a topic with a broker and a broker can also register a topic with a client.

The registration process involves sending the a register message containing the long topic name and a topic id will be returned.

This topic ID can then be used to publish messages.

The code for the Python client is shown below



Publishing Messages With an Established Connection

You can publish a message using:

  • A topic ID
  • A short topic name- 2 characters

You can a get a topic ID by either:

  • Subscribing to the long topic name.
  • Registering the Long topic name.
  • Using a pre-defined topic-id

As seen earlier the Subscribe and Register functions both return a topic ID that you use in place of the long topic name when publishing.

Publishing using a Topic Id

We will use the subscribe script and extend it to publish messages on each of the subscribed topics.

The subscribe and register functions both return a topic id for a long topic name which was topic id 1 and 2 respectively.

We can publish on the short topic name AA without prior registration.

The publish method accepts 4 parameters and returns the msgId as shown below:


topic=topic ID or short topic name
msg= the message payload
qos= 0,1,2 (optional defaults to 0)
retain = retained flag (optional default= False)

Publishing using the MQTT-SN-PUB tool

In the screen shot below you can see the command and the RSMB broker console.

Notice that the client has to register the topic before publishing. This is done automatically by the tool.

QOS Levels

MQTT-SN supports The same QOS levels as MQTT(0,1,2) with the same meaning as MQTT.

The RSMB broker doesn’t support QOS 2.

It also supports a fourth level called QOS -1 which allows a client to publish a message without first establishing a connection.

Publishing with QOS -1 or QOS 3

The reason it is also known as QOS 3 is because the QOS field uses 2 bits so:

00=qos 0
01=qos 1
10=qos 2
11=qos -1 or 3

When publishing with QOS of  -1 no connection message is sent.

You can see this in the broker trace below where I use MQTT_SN_PUB to  publish  without a connection using QOS of -1.


Demo Script

The demo script below uses the subscribe and register functions and then publishes messages using the topicid.

It isn’t the same as the one included in the client scripts.


Related tutorials and resources

Please rate? And use Comments to let me know more

Leave a Reply

Your email address will not be published. Required fields are marked *