JavaScript Required

We're sorry, but we doesn't work properly without JavaScript enabled.

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

Socket communication Using MQTT in Python

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.

Q. What is MQTT?

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

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

Q. Can you mention some use cases for above?

Below are some common use cases:

Q. Is MQTT a widely known standard of communication?

MQTT being a light weight 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. How 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 server.

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

So, if we were to use MQTT how would we design the solution for above problem? Let’s say each of the cabs are having a channel of format `location/` and every second’s these apps will publish the location on their channel; and if there is a server listening to 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:

Requirements:

Implementation:

Complete code below (using Public Eclipse Broker):

mqtt_client.py

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()

mqtt_server.py

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 setup a MQTT layer on top of an existing python app development services, and thereby advances you one step towards building highly scalable and reliable applications.

Extra configurations:

Hope this tutorial blog will help python Developers

Some of our clients

team