← Back to team overview

mosquitto-users team mailing list archive

process two types of message in single Python mosquito client

 

hi folks,

I am building a server using mosquito python client. As far as I know there are only one on_message callback function need to be defined. 

as the doc says here:
http://mosquitto.org/documentation/python/


Message callback

This is called when a message has been received by the client. The msg parameter is a MosquittoMessage object that contains all of the message information:

	• msg.mid – the integer message id
	• msg.topic – the topic to which the message was published
	• msg.payloadlen – the length in bytes of the payload (may be zero)
	• msg.payload – the message payload
	• msg.qos – the message quality of service level, 0, 1 or 2
	• msg.retain – set to true if the message was published as a “last known good” value
def on_message(mosq, obj, msg):
    print("Message received on topic "+msg.topic+" with QoS "+str(msg.qos)+" and payload "+msg.payload)
client.on_message = on_message

My question is: what if this client need to subscribe with 2 topics: A and B, and in this on_message function, when it is dealing with topic A messages, it actually need information from messages of topic B. 

Is there any way for this single client to subscribe to multiple topics, and process the messages from both in a single time sequence. like this:

1. it sub to topic A first
2. when it get a message on topic A, start to sub to topic B(actually topic B is generated dynamically via message on A) to check if there is any retain message
3. use  to decide how to process topic A messages.

Basically putting all these steps in one on_publish function. 

kind of twisted. :(

thanks a lot!
Horace






References