0% found this document useful (0 votes)
155 views1 page

Paho MQTT Chat

This document demonstrates how to create a simple chat application using MQTT. It shows the code for two MQTT clients that can publish and subscribe to messages on topics to allow users to send messages to each other in real-time. Client 1 subscribes to the "/baziramwabo" topic and publishes to "/gabriel", while Client 2 subscribes to "/gabriel" and publishes to "/baziramwabo". This allows the clients to exchange messages input by each user.

Uploaded by

mpeaceishimwe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views1 page

Paho MQTT Chat

This document demonstrates how to create a simple chat application using MQTT. It shows the code for two MQTT clients that can publish and subscribe to messages on topics to allow users to send messages to each other in real-time. Client 1 subscribes to the "/baziramwabo" topic and publishes to "/gabriel", while Client 2 subscribes to "/gabriel" and publishes to "/baziramwabo". This allows the clients to exchange messages input by each user.

Uploaded by

mpeaceishimwe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

< foreach Gabriel 's Desk Courses/Advanced Embedded Systems/MQTT/ MQTT Scenario 2

MQTT Scenario 2

With the help of MQTT, let's create a simple chat!

Client 1:
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):


print("Connected with result code "+str(rc))
client.subscribe("/baziramwabo")

def on_message(client, userdata, msg):


print("Baziramwabo: "+str(msg.payload.decode()))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("82.165.97.169", 1883, 60)

# Start the network loop


client.loop_start()

while True:
message = input("")
client.publish("/gabriel", message)

Client 2:
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):


print("Connected with result code "+str(rc))
client.subscribe("/gabriel")

def on_message(client, userdata, msg):


print("Gabriel: "+str(msg.payload.decode()))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("82.165.97.169", 1883, 60)

# Start the network loop


client.loop_start()

while True:
message = input("")
client.publish("/baziramwabo", message)

You might also like