MQTT is a publish and subscribe protocol with no direct connection between clients.
However many applications require a client to client type connection. Examples are:
- Chat
- Sensor or device control
This can be achieved in all versions of MQTT but it has been made easier in MQTTv5 with the introduction of request response in the publish payload.
In this tutorial we look at achieving the same in MQTTv3.1.1.
If you look at the simple diagram below which show a typical communication between MQTT clients.
Because there is no direct connection between client A and client B when any of the clients receives data it doesn’t know who sent it.
There are however several ways of overcoming this the main ones are:
- Include the client name in the payload data
- Use a topic that identifies the sending client
The most common implementation is using topic names.A typical design uses a topic structure of:
topic_base/dest_client/source_client
for two two clients called a and b.
Client A publishes to topic client_b using the topic topic_base/client_b/client_a.
Client B subscribes to topic_base/# and must extract the sender client topic and the destination topic from the received messages. It only replies to messages sent to itself.
Client B publishes to topic client_a using the topic topic_base/client_a/client_b.
Client A subscribes to topic_base/# subscribes to topic_base/# and must extract the sender client topic and the destination topic from the received messages. It only replies to messages sent to itself.
Example Python Scripts
You can download example scripts for client A and Client B. When you run them you should see messages on Client A from Client B and vice versa.
Although the scripts use Python the technique is applicable to all client types.
Related tutorials
- Receiving Messages with the Paho MQTT Python Client
- Checking Active MQTT Client Connections
- How to Send and Receive JSON Data Over MQTT with Python
- How to Send a File Using MQTT and Python
- How to Encrypt MQTT Payloads with Python – Example Code
Thx Steve for this enlightenment. Am working on a task to build a platform for monitoring and controlling ZHC493C IOT devices installed at different locations.
Kindly advise on preferable platform and programme to achieve this pls.
Hi
It depends on what you are comfortable with. My preferred platform is node-red.
https://stevesnoderedguide.com/
Rgds
Steve
Hi,
I want to send file from publisher to subscriber and vice versa publishing on two different topic.when i am doing it separately it is working but i want both publisher for topic1 and subscriber for topic2 in one file so how can i do that?
Sorry not quite grasped what you want can you clarify
Rgds
Steve
hi steve, i tried to transfer files through mqtt using mosquitto brocker.but it shows unable open file
There is a tutorial with code for file transfer here
http://www.steves-internet-guide.com/send-file-mqtt/
rgds
steve
Hi, Steve, I am trying to build communication between Raspberry Pi and my PC, can one of the clients also be the broker?
The broker and client can run on the same machines but you need two clients + a broker.
Rgds
Steve
Hi Steve,
Is there a way within mqtt v 5 for one client to publish to another client to verify it is online? If someone in the browser over websocket wants to verify their device is online they can publish to their device, which is subscribed to this online verification topic, and wait for the confirmation. This is possible with javascript intervals but I am wondering if there is anything protocl specific. I use both mqtt.js and paho python mqtt. Mqtt.js has a messageExpiryInterval option but I still dont know much about it. Thank you for any input!
No there is nothing in the protocol itself except the last will but the approach is to use a birth message on the client and then use the last will to update it I talk about the approach in this tutorial
http://www.steves-internet-guide.com/checking-active-mqtt-client-connections/
rgds
steve
broker=”192.168.1.68″
how to use this kind of Ip address in broker? when I try in my code its always gives me error what should i do?
You need to change the broker IP address to match your broker or use a public broker test.mosquitto.org
Hi Steve,
I am trying to run my first mqtt client program using python 3.8 on windows 10 & it was installed using the steps mentioned on your website. But running the program i get following error:
Traceback (most recent call last):
File “mqttclient.py”, line 23, in
client.on_connect = on_connect
NameError: name ‘client’ is not defined
When i checked paho-mqtt for installation, i got this below:
c:\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\paho\mqtt
This is my code:
import paho.mqtt.client as mqtt mqtt_broker_ip=”mqtt.eclipse.org” mqtt_broker_port=1883 mqtt_subscription_topic_name=”Test”
def on_connect(client, userdata, flags, rc): print(“Connected with result code “+str(rc))
def on_message(client, userdata, msg): print(msg.topic+” “+str(msg.payload))
mqtt_client=mqtt.Client(“P1”) client.on_connect = on_connect client.on_message = on_message
print(“connecting to broker”) client.connect(broker_address,mqtt_broker_port,keepalive=60)
client.loop_start()
print(“Subscribing to topic”) client.subscribe(mqtt_subscription_topic_name) print(“Publishing message to topic”)
client.publish(mqtt_subscription_topic_name,”OFF”)
client.loop_forever()
Can you tell me where i am making a mistake(using explicit path of the mqtt also getting the same error)?
Hi
You shouldn’t use loop_forever and looP-start in same script. Replace the lopp_forever with a while loop to hold the script.
while True:
pass
What format is your download? doesn’t look like txt.
Hi
It is a zipped file
rgds
steve