Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take two weeks Trial! Try Now

Socket communication Using MQTT in Python

Q. What is Socket communication?

A protocol to send bi-directional messages (data) from one point to another, unlike the traditional communication protocols like REST, socket communication works by establishing a channel where the publisher can publish to that channel and receivers can listen to that channel. In other words, it’s a way of publishing subscribing method.

Socket communication Using MQTT in Python

Q. What is MQTT?

MQTT (Message Queuing Telemetry Transport) is a very lightweight publish-subscribe protocol that enables us to build systems capable of publishing (server) and receiving messages (client).

Let’s go with an example: Assume users A, B and C is listening to a channel updates/and lets an X publishes a message New Update: Please update your application to 2.2on to updates/ channel, the user A, B, and C would immediately get to see the message.

Q. Can you mention some use cases for the above?

Below are some common use cases:

Q. Is MQTT a widely known standard of communication?

MQTT being a lightweight protocol sits aside your existing server-side and brings you to a lot of capabilities of pub-sub service.

Dive Deep with a Use Case:

Let’s think of a scenario where you are building a fleet (say Cab) tracking application where the location is updated from a mobile application every 5 seconds (say). And you have a dashboard on a web application to show these cabs. What would you think about designing it?

The initial call would be to have a REST API to trigger the location update from the Android every 5 seconds to the server? Good catch. But this is not something scalable or reliable going forward. Think of the number of HTTP requests hitting the server along with the load handling capability of the server.

Consider the dashboard also makes API calls for those cars for each user seeing the dashboard. You wouldn’t be happier with that figure and that’s why MQTT comes to the rescue.

So, if we were to use MQTT how would we design the solution for the above problem? Let’s say each of the cabs are having a channel of format location/ and every second these apps will publish the location on their channel; if there is a server listening to the channel with a pattern location/*, these updates would be received at the server-side. Sounds pretty cool?

A question to ask yourself. How would you track an individual cab’s location instead of all cars? Well, the answer is to change the listening channel to location/.

How to setup MQTT service using Python:



Complete code below (using Public Eclipse Broker):


import paho.mqtt.client as mqtt MQTT_BROKER_HOST = 'mqtt.eclipse.org' MQTT_BROKER_PORT = 1883 MQTT_KEEP_ALIVE_INTERVAL = 60 def on_connect(client, userdata, flags, rc): print("Connected with result code " + str(rc)) client.subscribe("location/123") def on_message(client, userdata, msg): print("Message Recieved. ", msg.payload.decode()) client = mqtt.Client() client.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT, MQTT_KEEP_ALIVE_INTERVAL) client.on_connect = on_connect client.on_message = on_message client.loop_forever()


import paho.mqtt.client as mqtt client = mqtt.Client() client.connect("mqtt.eclipse.org",1883,60) client.publish("location/123", "Bengaluru, India"); client.disconnect();

By now, I assume you know how to set up an MQTT layer on top of existing python development services, and thereby advancing you one step towards building highly scalable and reliable applications.

Extra configurations:

Software Development Team
Need Software Development Team?

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs


NSS Note
Some of our clients