after reconnection subscriber does not getting messages

684 views Asked by At

N.B. I am using EMQX as broker and python-paho as the client library. I am not sure which one is responsible for this problem.

If the publisher does not send any message for some time, the subscriber gets disconnected but also gets re-connected again (since loop_forever automatically handles reconnection). But after reconnection, if the producer starts to send data again, the auto-reconnected server does not get any messages. In that case, the subscriber needs to start again (manually reconnection)

My Subscriber

import paho.mqtt.client as mqttClient
import json
import datetime

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

    if rc == 0:
        print("Connected to broker")

    else:
        print("Connection failed")

def on_disconnect(client, userdata, rc):
    if rc != 0:
        print("Unexpected MQTT disconnection. Will auto-reconnect")
    

def on_message(client, userdata, message):
    print(str(datetime.datetime.now())+str(message.payload) + "\n")


#broker_address= "3.121.233.176" 
broker_address= "address_of_the_broker" 
port = 1883                     
client = mqttClient.Client("clientLB1")               #create new instance
client.on_connect= on_connect                      #attach function to callback
client.on_message= on_message                      #attach function to callback
client.on_disconnect = on_disconnect
client.connect(broker_address,port,60)
client.subscribe("xdk1") #subscribe
client.loop_forever() #then keep listening forever
 

My Publisher:

import paho.mqtt.client as paho
import time
from random import random

def on_publish(client, userdata, mid):
    print("mid: "+str(mid))
 
client = paho.Client()
client.on_publish = on_publish
#client.connect("35.157.39.224", 1883)
client.connect("LBmqtt-1826452731.eu-central-1.elb.amazonaws.com", 1883)
client.loop_start()

while True:
    temperature = random()
    (rc, mid) = client.publish("xdk1", str(temperature), qos=1)
    time.sleep(.1)

How to solve this problem?

1

There are 1 answers

0
Mainul On BEST ANSWER

Solved it by

client = mqttClient.Client("client_name",clean_session=False) 

from the doc:

clean_session: a boolean that determines the client type. If True, the broker will remove all information about this client when it disconnects. If False, the client is a durable client and subscription information and queued messages will be retained when the client disconnects.