Feature - Callback for receiving message data - Raw - MQTT - libmqttv3a - asynchronous version

107 views
Skip to first unread message

Paulo Balbino

unread,
Oct 7, 2019, 6:45:53 PM10/7/19
to MQTT
Dear All,

  I am using Paho MQTT Ayncronous version ( libmqttv3a) and I am trying to find a way to receive messages in similar way that Mosquitto does, 
mosquitto has 2 functions mosquitto_message_callback_set and mosquitto_subscribe_callback_set, the first is for receiving messages from that came from
the broker and the second when the broker responds to a  subscription request.

In case of Paho I am only able to find one similar to the second, MQTTAsync_setCallbacks 4th parameter (MQTTAsync_messageArrived callback)
This function only handles Subscription responses.

I am searching for something similar to mosquitto mosquitto_message_callback_set   for use on Paho MQTT Async Library, any ideas?


Paho documentation

This is a callback function. The client application must provide an implementation of this function to enable asynchronous receipt of messages. The function is registered with the client library by passing it as an argument to MQTTAsync_setCallbacks(). It is called by the client library when a new message that matches a client subscription has been received from the server. This function is executed on a separate thread to the one on which the client application is running.

Parameters
contextA pointer to the context value originally passed to MQTTAsync_setCallbacks(), which contains any application-specific context.
topicNameThe topic associated with the received message.
topicLenThe length of the topic if there are one more NULL characters embedded in topicName, otherwise topicLen is 0. If topicLen is 0, the value returned by strlen(topicName) can be trusted. If topicLen is greater than 0, the full topic name can be retrieved by accessing topicName as a byte array of length topicLen.
messageThe MQTTAsync_message structure for the received message. This structure contains the message payload and attributes.
Returns
This function must return a boolean value indicating whether or not the message has been safely received by the client application. Returning true indicates that the message has been successfully handled. Returning false indicates that there was a problem. In this case, the client library will reinvoke MQTTAsync_messageArrived() to attempt to deliver the message to the application again.


Mosquitto Documentation
/*
 * Function: mosquitto_message_callback_set
 *
 * Set the message callback. This is called when a message is received from the
 * broker.
 * 
 * Parameters:
 *  mosq -       a valid mosquitto instance.
 *  on_message - a callback function in the following form:
 *               void callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
 *
 * Callback Parameters:
 *  mosq -    the mosquitto instance making the callback.
 *  obj -     the user data provided in <mosquitto_new>
 *  message - the message data. This variable and associated memory will be
 *            freed by the library after the callback completes. The client
 *            should make copies of any of the data it requires.
 *
 * See Also:
 * <mosquitto_message_copy>
 */
libmosq_EXPORT void mosquitto_message_callback_set(struct mosquitto *mosq, void (*on_message)(struct mosquitto *, void *, const struct mosquitto_message *));

/*
 * Function: mosquitto_subscribe_callback_set
 *
 * Set the subscribe callback. This is called when the broker responds to a
 * subscription request.
 * 
 * Parameters:
 *  mosq -         a valid mosquitto instance.
 *  on_subscribe - a callback function in the following form:
 *                 void callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
 *
 * Callback Parameters:
 *  mosq -        the mosquitto instance making the callback.
 *  obj -         the user data provided in <mosquitto_new>
 *  mid -         the message id of the subscribe message.
 *  qos_count -   the number of granted subscriptions (size of granted_qos).
 *  granted_qos - an array of integers indicating the granted QoS for each of
 *                the subscriptions.
 */
libmosq_EXPORT void mosquitto_subscribe_callback_set(struct mosquitto *mosq, void (*on_subscribe)(struct mosquitto *, void *, int, int, const int *));

Reply all
Reply to author
Forward
0 new messages